死锁

2021/9/17 23:09:59

本文主要是介绍死锁,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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);

}
}
});



这篇关于死锁的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程