最近复习Windows技术的时候看到了线程异步的知识。这里介绍其中的AutoResetEvent和ManualResetEvent的区别。

AutoResetEvent

查阅微软官方英文文档
微软官方给了下面的例子。看微软这个AutoReset的例子,他的ThreadProc方法里面有event_1和event_2的对象call 了waitOne。首先因为创建的event_1对象初始状态是true,所以第一个线程跑到event_1.waitone()这一行的时候是直接通行了的,然后走到了event_2.watieOne停了下来。
而后续的两个线程因为也要跑event_1.WaitOne这一行,因为第一个线程在call WaitOne的时候已经自动Reset了event_1的信号,把信号改成了false,也就是关闭了。所以两个线程都要等set方法将信号置为true。
于是在Main的下一个for(int i=0;i<2;i++)循环中,来纳许调用了两次event_1.set方法。
循环执行了第一次set让第二个线程同行,然后信号被置为false。第二次set让第三个线程通行,信号又被自动置为false。
event_2也是这个道理。

using System;
using System.Threading;// Visual Studio: Replace the default class in a Console project with
//                the following class.
class Example
{private static AutoResetEvent event_1 = new AutoResetEvent(true);private static AutoResetEvent event_2 = new AutoResetEvent(false);static void Main(){Console.WriteLine("Press Enter to create three threads and start them.\r\n" +"The threads wait on AutoResetEvent #1, which was created\r\n" +"in the signaled state, so the first thread is released.\r\n" +"This puts AutoResetEvent #1 into the unsignaled state.");Console.ReadLine();for (int i = 1; i < 4; i++){Thread t = new Thread(ThreadProc);t.Name = "Thread_" + i;t.Start();}Thread.Sleep(250);for (int i = 0; i < 2; i++){Console.WriteLine("Press Enter to release another thread.");Console.ReadLine();event_1.Set();Thread.Sleep(250);}Console.WriteLine("\r\nAll threads are now waiting on AutoResetEvent #2.");for (int i = 0; i < 3; i++){Console.WriteLine("Press Enter to release a thread.");Console.ReadLine();event_2.Set();Thread.Sleep(250);}// Visual Studio: Uncomment the following line.//Console.Readline();}static void ThreadProc(){string name = Thread.CurrentThread.Name;Console.WriteLine("{0} waits on AutoResetEvent #1.", name);event_1.WaitOne();Console.WriteLine("{0} is released from AutoResetEvent #1.", name);Console.WriteLine("{0} waits on AutoResetEvent #2.", name);event_2.WaitOne();Console.WriteLine("{0} is released from AutoResetEvent #2.", name);Console.WriteLine("{0} ends.", name);}
}/* This example produces output similar to the following:Press Enter to create three threads and start them.
The threads wait on AutoResetEvent #1, which was created
in the signaled state, so the first thread is released.
This puts AutoResetEvent #1 into the unsignaled state.Thread_1 waits on AutoResetEvent #1.
Thread_1 is released from AutoResetEvent #1.
Thread_1 waits on AutoResetEvent #2.
Thread_3 waits on AutoResetEvent #1.
Thread_2 waits on AutoResetEvent #1.
Press Enter to release another thread.Thread_3 is released from AutoResetEvent #1.
Thread_3 waits on AutoResetEvent #2.
Press Enter to release another thread.Thread_2 is released from AutoResetEvent #1.
Thread_2 waits on AutoResetEvent #2.All threads are now waiting on AutoResetEvent #2.
Press Enter to release a thread.Thread_2 is released from AutoResetEvent #2.
Thread_2 ends.
Press Enter to release a thread.Thread_1 is released from AutoResetEvent #2.
Thread_1 ends.
Press Enter to release a thread.Thread_3 is released from AutoResetEvent #2.
Thread_3 ends.*/

ManualResetEvent

查阅微软官方英文文档
微软官方给了下面这个例子。其实它和AutoReset最大的区别可以从字面上理解,AutoResetEvent的实例在调用WaitOne方法之后会自动将状态置为false,而ManualResetEvent是不会的,如果ManualResetEvent对象的实例状态为true,WaiteOne只起一个检查状态的作用,即使到了调用WatieOne方法的时候,因为状态是true他也不会停下来,而是继续运行,也不会更改状态为false;只有在调用了Reset方法后,实例的状态才会变为false,这样执行到WaitOne的时候才会停下来。

using System;
using System.Threading;public class Example
{// mre is used to block and release threads manually. It is// created in the unsignaled state.private static ManualResetEvent mre = new ManualResetEvent(false);static void Main(){Console.WriteLine("\nStart 3 named threads that block on a ManualResetEvent:\n");for(int i = 0; i <= 2; i++){Thread t = new Thread(ThreadProc);t.Name = "Thread_" + i;t.Start();}Thread.Sleep(500);Console.WriteLine("\nWhen all three threads have started, press Enter to call Set()" +"\nto release all the threads.\n");Console.ReadLine();mre.Set();Thread.Sleep(500);Console.WriteLine("\nWhen a ManualResetEvent is signaled, threads that call WaitOne()" +"\ndo not block. Press Enter to show this.\n");Console.ReadLine();for(int i = 3; i <= 4; i++){Thread t = new Thread(ThreadProc);t.Name = "Thread_" + i;t.Start();}Thread.Sleep(500);Console.WriteLine("\nPress Enter to call Reset(), so that threads once again block" +"\nwhen they call WaitOne().\n");Console.ReadLine();mre.Reset();// Start a thread that waits on the ManualResetEvent.Thread t5 = new Thread(ThreadProc);t5.Name = "Thread_5";t5.Start();Thread.Sleep(500);Console.WriteLine("\nPress Enter to call Set() and conclude the demo.");Console.ReadLine();mre.Set();// If you run this example in Visual Studio, uncomment the following line://Console.ReadLine();}private static void ThreadProc(){string name = Thread.CurrentThread.Name;Console.WriteLine(name + " starts and calls mre.WaitOne()");mre.WaitOne();Console.WriteLine(name + " ends.");}
}/* This example produces output similar to the following:Start 3 named threads that block on a ManualResetEvent:Thread_0 starts and calls mre.WaitOne()
Thread_1 starts and calls mre.WaitOne()
Thread_2 starts and calls mre.WaitOne()When all three threads have started, press Enter to call Set()
to release all the threads.Thread_2 ends.
Thread_0 ends.
Thread_1 ends.When a ManualResetEvent is signaled, threads that call WaitOne()
do not block. Press Enter to show this.Thread_3 starts and calls mre.WaitOne()
Thread_3 ends.
Thread_4 starts and calls mre.WaitOne()
Thread_4 ends.Press Enter to call Reset(), so that threads once again block
when they call WaitOne().Thread_5 starts and calls mre.WaitOne()Press Enter to call Set() and conclude the demo.Thread_5 ends.*/

理解AutoResetEvent与ManualResetEvent的区别相关推荐

  1. 简单理解AutoResetEvent及ManualResetEvent

    AutoResetEvent: 构造方法:AutoResetEvent对象有两个状态,有信号和无信号,在初始化时可以分别用true和false指定 WaitOne:该对象在无信号时调用WaitOne方 ...

  2. 对AutoResetEvent和ManualResetEvent的理解

    一.作用 AutoResetEvent和ManualResetEvent可用于控制线程暂停或继续,拥有重要的三个方法:WaitOne.Set和Reset. 这三个方法的官方定义并不好理解,什么终止.非 ...

  3. 个人对AutoResetEvent和ManualResetEvent的理解

    仅个人见解,不对之处请指正,谢谢. 一.作用 AutoResetEvent和ManualResetEvent可用于控制线程暂停或继续,拥有重要的三个方法:WaitOne.Set和Reset. 这三个方 ...

  4. AutoResetEvent和ManualResetEvent

    首先说说线程的终止状态和非终止状态.AutoResetEvent和ManualResetEvent的构造函数中,都有bool变量来指明线程的终止状态和非终止状态.true表示终止状态,false表示非 ...

  5. C#笔记20:多线程之线程同步中的信号量AutoResetEvent和ManualResetEvent

    C#笔记20:多线程之线程同步中的信号量AutoResetEvent和ManualResetEvent 本章概要: 1:终止状态和非终止状态 2:AutoResetEvent和ManualResetE ...

  6. AutoResetEvent与ManualResetEvent区别

    在.Net多线程编程中,AutoResetEvent和ManualResetEvent这两个类经常用到, 他们的用法很类似,但也有区别.Set方法将信号置为发送状态,Reset方法将信号置为不发送状态 ...

  7. c# AutoResetEvent和ManualResetEvent

    网上有很多AutoResetEvent和ManualResetEvent的详细介绍,在这里不做过多详细的解释,写下自己的一点心得留作备忘. AutoResetEvent和ManualResetEven ...

  8. 多线程间通信之AutoResetEvent和ManualResetEvent的原理分析

    AutoResetEvent 允许线程通过发信号互相通信. 通常,当线程需要独占访问资源时使用该类. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号. 如果 AutoRe ...

  9. 【深度好文】多线程之WaitHandle--派生EventWaitHandle事件构造-》AutoResetEvent、ManualResetEvent...

    AutoResetEvent/ManualResetEvent 都是继承自 EventWaitHandle ,EventWaitHandle继承自WaitHandle. 在讨论这个问题之前,我们先了解 ...

  10. AutoResetEvent 与 ManualResetEvent

    闲来无事,把AutoResetEvent 与 ManualResetEvent 两个Event进行了一下实践Demo,毕竟以后会遇到多线程的开发问题,现在有时间把它们好好的先试验一下,这样印象会更深刻 ...

最新文章

  1. 前端token刷新并发处理
  2. 扒中台的“祖坟”!对不起,Supercell的“中台”,你学不会
  3. 目标检测 - 如何在图片中标记Annotations中的坐标信息?
  4. day4 数组的初始化和练习
  5. ubuntu下arpoison安装
  6. C#中用ToString方法格式化时间
  7. 纯电动两档箱实际项目模型,本模型基于Cruise软件和搭建完成,本资料包包含所有源文件
  8. 机器学习——sklearn实现决策树(隐形眼镜预测和鸢尾花分类)
  9. 网络小说海外“走红”的启示
  10. SPSS(七)非线性回归过程
  11. 经典对抗攻击Deepfool原理详解与代码解读
  12. mysql获取年龄_sql获取时间、年龄
  13. windows 7 数据执行保护 开启关闭方法
  14. python3代码换行与不换行问题
  15. 中央电大c 语言程序设计本科试题,中央电大本科C语言程序设计试题2013年1月.pdf...
  16. Android音频架构概览
  17. sscom串口网络数据调试器使用post方法向华为云obs桶上传文件和图片
  18. Linux系统用gcc编写C语言程序
  19. 电弧光保护系统 就选汉光HKHB-608智能电弧光保护系统
  20. vue给url 中文参数 添加编码解码

热门文章

  1. JDY-18+STM32的蓝牙APP控制 PWM灯光
  2. NRF24L01调试的一些经验之谈
  3. IANA已注册的TCP/UDP/SCTP/DCCP传输协议端口及服务名称
  4. 安卓仿苹果键盘输入法_苹果手机键盘背景图 安卓仿iphone输入法
  5. 遗传算法求解多约束、多类型车辆、多目标优化的车辆路径问题
  6. 发布QtCsv文件转语言翻译文件工具
  7. php计算QQ音乐guid,QQ音乐API分析2017
  8. 初中计算机期末质量分析,初中信息技术教学感悟随笔
  9. CentOS7安装搜狗输入法
  10. Vapnik-Chervonenkis Dimension 理解