Wait: 暂时的释放资源锁,然后该线程进入”等待队列“中,那么自然别的线程就能获取到资源锁。
Pulse: 唤醒“等待队列”中的线程,使其进入就绪队列,那么当时被Wait的线程就重新获取到了锁。此函数不会释放当前资源锁。
class Program
{
private static int Count = 0;
private static object lock_obj = new object();
static void Main(string[] args)
{
Thread a = new Thread(AAA);
a.Start();
Thread b = new Thread(BBB);
b.Start();
Console.ReadLine();
}
static void AAA()
{
lock (lock_obj)
{
Console.WriteLine("aaa 开始 ");
Monitor.Wait(lock_obj);
Monitor.Pulse(lock_obj);
Console.WriteLine("aaa 11 ");
}
}
static void BBB()
{
lock (lock_obj)
{
Console.WriteLine("bbb 开始 ");
Monitor.Pulse(lock_obj);
Monitor.Wait(lock_obj);
Console.WriteLine("bbb 11 ");
}
}
}