- threadA uses lock1 and then lock2
- threadB uses lock2 with then lock1
using System;
using System.Threading;
namespace pc.DeadlockExample
{
class Program
{
static void Main(string[] args)
{
object lock1 = new object();
object lock2 = new object();
var ThreadA = new Thread(() =>
{
lock (lock1)
{
Console.WriteLine($"I'm ThreadA I'm using lock A");
lock (lock2)
{
Console.WriteLine($"I'm ThreadA I'm using lock A & B");
}
}
});
var ThreadB = new Thread(() =>
{
lock (lock2)
{
Console.WriteLine($"I'm ThreadB I'm using lock B");
lock (lock1)
{
Console.WriteLine($"I'm ThreadB I'm using locks A & B");
}
}
});
ThreadA.Start();
ThreadB.Start();
}
}
}
using System;
using System.Threading;
namespace pc.DeadlockExample
{
class Program
{
static void Main(string[] args)
{
object lock1 = new object();
object lock2 = new object();
var ThreadA = new Thread(() =>
{
lock (lock1)
{
Console.WriteLine($"I'm ThreadA I'm using lock A");
lock (lock2)
{
Console.WriteLine($"I'm ThreadA I'm using lock A & B");
}
}
});
var ThreadB = new Thread(() =>
{
lock (lock1)
{
Console.WriteLine($"I'm ThreadB I'm using lock B");
lock (lock2)
{
Console.WriteLine($"I'm ThreadB I'm using locks A & B");
}
}
});
ThreadA.Start();
ThreadB.Start();
}
}
}