static readonly myLock keyA = new myLock("A");
static readonly myLock keyB = new myLock("B");
static void Main(string[] args)
{
Task.Run(() => {
lock (keyA)
{
Console.WriteLine("线程:"+Thread.CurrentThread.ManagedThreadId.ToString() + "进来房间:A");
// Thread.Sleep(2000);
lock (keyB)
{
Console.WriteLine("线程:" + Thread.CurrentThread.ManagedThreadId.ToString() + "进来房间:B," + keyB.str);
}
}
});
Task.Run(() =>
{
lock (keyB)
{
Console.WriteLine("线程:" + Thread.CurrentThread.ManagedThreadId.ToString() + "进来房间:B,拿着钥匙" + keyB.str);
//Thread.Sleep(3000);
lock (keyA)
{
Console.WriteLine("线程:" + Thread.CurrentThread.ManagedThreadId.ToString() + "进来房间:A," + keyB.str);
}
}
});