谁能举个例子说明在C#语言中线程死锁是如何引起的?
发布于 2010-03-10 02:19:27
static object object1 = new object();
static object object2 = new object();
public static void ObliviousFunction()
{
lock (object1)
{
Thread.Sleep(1000); // Wait for the blind to lead
lock (object2)
{
}
}
}
public static void BlindFunction()
{
lock (object2)
{
Thread.Sleep(1000); // Wait for oblivion
lock (object1)
{
}
}
}
static void Main()
{
Thread thread1 = new Thread((ThreadStart)ObliviousFunction);
Thread thread2 = new Thread((ThreadStart)BlindFunction);
thread1.Start();
thread2.Start();
while (true)
{
// Stare at the two threads in deadlock.
}
}
https://stackoverflow.com/questions/2411410
复制相似问题