考虑这段代码:
public class Test
{
public void Print()
{
lock (this)
{
System.Threading.Thread.Sleep(10000);
Console.WriteLine("Print");
}
}
public static void Somthing()
{
Console.WriteLine("Somthing");
}
}
在print方法 Ilock中,该类Somthing是一个静态方法。我希望在那Somthing之后调用时Print,Somthing单独运行线程,因为我没有Test调用实例Somthing。
private static void Main(string[] args)
{
var test = new Test();
test.Print();
Test.Somthing();
}
但是当写上面的代码时,Test锁定然后调用Somthing。
为什么编译器有这种行为?