ManualResetEvent使用 AutoResetEvent 类
假设一个使用场景:
1. 有一台仪器,需要预热后才能使用;
2. 需要使用线程操作这台仪器;
static bool isReady = false; public static void Test1() { Thread th = new Thread(() => { while (!isReady) ; //不断执行此语句 Console.WriteLine("开始工作"); }); th.Start(); Console.WriteLine("按任意键继续..."); Console.ReadKey(); isReady = true; Console.ReadKey(); }
使用ManualResetEvent后:
//注意到这里设置为false, static ManualResetEvent manual = new ManualResetEvent(false); public static void Test1() { Thread th = new Thread(() => { manual.WaitOne(); Console.WriteLine("开始工作"); }); th.Start(); Console.WriteLine("按任意键继续..."); Console.ReadKey(); manual.Set(); Console.ReadKey(); }
性能差距居然如此巨大!
再看一下,改为:
static ManualResetEvent manual = new ManualResetEvent(true);
manual.WaitOne(); //这句不再进行阻塞了
也就是说:
static ManualResetEvent manual = new ManualResetEvent(false); //代表一开始要进行阻塞,相当于 isReady = false
manual.Set(); //相当于isReady = true
manual.WaitOne(); //while(!isReady);
还有: manual.Reset(); ,相当于又 isReady = false;
对应还有
MSDN详解: https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.autoresetevent?view=netframework-4.8
就是在manual.WaitOne()之后,自动manual.Reset()
static ManualResetEvent manual = new ManualResetEvent(false); public static void Test1() { for (int i = 0; i < 5; i++) { Thread th = new Thread(() => { Console.WriteLine("线程" + Thread.CurrentThread.Name + "等待"); manual.WaitOne(); Console.WriteLine("线程" + Thread.CurrentThread.Name + "开始工作"); }); th.Name = i.ToString(); th.Start(); } Console.WriteLine("按回车键继续..."); Console.ReadLine(); manual.Set(); Console.ReadKey(); }
在ManualResetEvent时,一旦执行Set()后,所有的WaitOne()都掠过了;
改成:static AutoResetEvent manual = new AutoResetEvent(false);
只放任一个线程。
更多: