c#死锁示例代码

void Main()
{
    object obj1 = new object();
    object obj2 = new object();
    
    var t1 = new Thread(delegate(object p)
    {
        lock(obj1)
        {
            Thread.Sleep(5000);
            lock(obj2)
            {
                Console.WriteLine("Thread1 executed.");
            }
        }
    });
    
    t1.Start();
    
    
    var t2 = new Thread(delegate(object p)
    {
        lock(obj2)
        {
            Thread.Sleep(5000);
            lock(obj1)
            {
                Console.WriteLine("Thread2 executed.");
            }
        }
    });
    
    t2.Start();
    
    t1.Join();
    t2.Join();
    
    Console.WriteLine("over");
}

 

posted on 2018-07-17 17:41  空明流光  阅读(279)  评论(0编辑  收藏  举报

导航