string a="abc";lock(a){....};在實(shí)際使用中我發(fā)現(xiàn)鎖定的不是a對(duì)象,而是以a對(duì)象作為標(biāo)識(shí)的{}內(nèi)的代碼塊.. 但發(fā)現(xiàn)資料上是像圖中那樣說的,是我沒理解正確呢還是資料錯(cuò)誤?請(qǐng)高手解惑.using System;using System.Threading;class Program
{
static void Main(string[] args)
{
Student s = new Student { Name="yh"};
Thread t=new Thread (new ThreadStart(()=>{lock (s) { Thread.Sleep(100000); }}));
t.Start();
Thread.Sleep(1000);
Console.WriteLine(s.Name);//這里沒有等鎖的線程結(jié)束才執(zhí)行..所以我覺得資料說的不對(duì)..你們認(rèn)為呢?
Console.ReadKey();
}
}internal class Student
{ public string Name { get; set; }
C# 關(guān)于lock關(guān)鍵字的疑惑...高手請(qǐng)進(jìn)...
揚(yáng)帆大魚
2018-07-26 12:05:17