标签:超时 obj logs oid while 应用程序 str span lock
static void Main() { myObject1 = new object(); myObject2 = new object(); new Thread(Test1).Start(); new Thread(Test2).Start(); Console.ReadKey(); } private static void Test1() { while (true) { lock (myObject1) { lock (myObject2) { myObject1 = 1; Console.WriteLine(myObject1.ToString()); myObject2 = 1; Console.WriteLine(myObject2.ToString()); } } } } private static void Test2() { while (true) { lock (myObject2) { lock (myObject1) { myObject1 = 2; Console.WriteLine(myObject1.ToString()); myObject2 = 2; Console.WriteLine(myObject2.ToString()); } } } }
为了避免死锁,可以在应用程序的体系架构中一开始就设计好锁定顺序,也可以为锁定定义超时时间
标签:超时 obj logs oid while 应用程序 str span lock
原文地址:http://www.cnblogs.com/chenjiachuan/p/7827220.html