using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { EventWaitTest zhangsan = new EventWaitTest("张三"); EventWaitTest lisi = new EventWaitTest("李四"); Thread t1 = new Thread(new ThreadStart(zhangsan.Consume)); Thread t2 = new Thread(new ThreadStart(lisi.Consume)); Thread t3 = new Thread(new ThreadStart(EventWaitTest.Product)); t1.Start(); t2.Start(); t3.Start(); Console.Read(); } } public class EventWaitTest { private string name; //顾客姓名 //private static AutoResetEvent eventWait = new AutoResetEvent(false); private static ManualResetEvent eventWait = new ManualResetEvent(false); private static ManualResetEvent eventOver = new ManualResetEvent(false); public EventWaitTest(string name) { this.name = name; } public static void Product() { Console.WriteLine("服务员:厨师在做菜呢,两位稍等"); Thread.Sleep(2000); Console.WriteLine("服务员:宫爆鸡丁好了"); eventWait.Set(); while (true) { if (eventOver.WaitOne(1000, false)) { Console.WriteLine("服务员:两位请买单"); eventOver.Reset(); } } } public void Consume() { while (true) { if (eventWait.WaitOne(1000, false)) { Console.WriteLine(this.name + ":开始吃宫爆鸡丁"); Thread.Sleep(2000); Console.WriteLine(this.name + ":宫爆鸡丁吃光了"); eventWait.Reset(); eventOver.Set(); break; } else { Console.WriteLine(this.name + ":等着上菜无聊先玩会手机游戏"); } } } }}