C#多线程之二:ManualResetEvent和AutoResetEvent
来源:互联网 发布:残疾人就业歧视数据 编辑:程序博客网 时间:2024/05/18 18:54
初次体验
ManualResetEvent和AutoResetEvent主要负责多线程编程中的线程同步;以下一段是引述网上和MSDN的解析:
在.Net多线程编程中,AutoResetEvent和ManualResetEvent这两个类经常用到, 他们的用法很类似,但也有区别。
Set方法将信号置为发送状态,当信号处于发送状态时,该线程会得到信号, 继续向下执行
Reset方法将信号置为不发送状态,
WaitOne等待信号的发送。
可以通过构造函数的参数值来决定其初始状态,若为true则非阻塞状态,为false为阻塞状态。
如果某个线程调用WaitOne方法,则当信号处于发送状态时,该线程会得到信号, 继续向下执行。
其区别就在调用后,AutoResetEvent.WaitOne()每次只允许一个线程进入,当某个线程得到信号后,AutoResetEvent会自动又将信号置为不发送状态,则其他调用WaitOne的线程只有继续等待.也就是说,AutoResetEvent一次只唤醒一个线程;而ManualResetEvent则可以唤醒多个线程,因为当某个线程调用了ManualResetEvent.Set()方法后,其他调用WaitOne的线程获得信号得以继续执行,而ManualResetEvent不会自动将信号置为不发送。也就是说,除非手工调用了ManualResetEvent.Reset()方法,则ManualResetEvent将一直保持有信号状态,ManualResetEvent也就可以同时唤醒多个线程继续执行。
本质上AutoResetEvent.Set()方法相当于ManualResetEvent.Set()+ManualResetEvent.Reset();
因此AutoResetEvent一次只能唤醒一个线程,其他线程还是堵塞
生动示例
用一个三国演义的典故来写段示例代码:
话说曹操率领80W大军准备围剿刘备和孙权,面对敌众我寡的情况,诸葛亮与周瑜想到了一个妙计,用装满火药桶的大船去冲击曹操连在一起的战船,计划都安排好了,可谓“万事俱备 只欠东风”。
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace Test{ class Program { //默认信号为不发送状态 private static ManualResetEvent mre = new ManualResetEvent(false); static void Main(string[] args) { EastWind wind = new EastWind(mre); //启动东风的线程 Thread thd = new Thread(new ThreadStart(wind.WindComming)); thd.Start(); mre.WaitOne();//万事俱备只欠东风,事情卡在这里了,在东风来之前,诸葛亮没有进攻 //东风到了,可以进攻了 Console.WriteLine("诸葛亮大吼:东风来了,可以进攻了,满载燃料的大船接着东风冲向曹操的战船"); Console.ReadLine(); } } /// <summary> /// 传说中的东风 /// </summary> class EastWind { ManualResetEvent _mre; /// <summary> /// 构造函数 /// </summary> /// <param name="mre"></param> public EastWind(ManualResetEvent mre) { _mre = mre; } /// <summary> /// 风正在吹过来 /// </summary> public void WindComming() { Console.WriteLine("东风正在吹过来"); for (int i = 0; i <= 5; i++) { Thread.Sleep(500); Console.WriteLine("东风吹啊吹,越来越近了..."); } Console.WriteLine("东风终于到了"); //通知诸葛亮东风已到,可以进攻了,通知阻塞的线程可以继续执行了 _mre.Set(); } }}
运行结果:
实例二:
有时候只需要等待一定时间,就需要执行下面动作,而不是一直等下去
static void Main(string[] args) { EastWind wind = new EastWind(mre); //启动东风的线程 Thread thd = new Thread(new ThreadStart(wind.WindComming)); thd.Start(); // mre.WaitOne();//万事俱备只欠东风,事情卡在这里了,在东风来之前,诸葛亮没有进攻 mre.WaitOne(2000); //等待2s,如果还不来风,就进攻 //东风到了,可以进攻了 Console.WriteLine("诸葛亮大吼:东风来了,可以进攻了,满载燃料的大船接着东风冲向曹操的战船"); Console.ReadLine(); }
运行结果:
- C#多线程之二:ManualResetEvent和AutoResetEvent
- C#多线程之二:ManualResetEvent和AutoResetEvent
- C#多线程之二:ManualResetEvent和AutoResetEvent
- C#多线程之二:ManualResetEvent和AutoResetEvent
- C#多线程之二:ManualResetEvent和AutoResetEvent
- C#多线程之ManualResetEvent和AutoResetEvent
- C# 多线程 AutoResetEvent和ManualResetEvent
- 多线程之AutoResetEvent和ManualResetEvent
- c#多线程之mutex,semaphore,autoresetevent,manualResetevent
- 多线程ManualResetEvent和AutoResetEvent
- 多线程编程之 AutoResetEvent和ManualResetEvent
- C#老生常谈之AutoResetEvent和ManualResetEvent
- C#笔记20:多线程之线程同步中的信号量AutoResetEvent和ManualResetEvent
- C#笔记20:多线程之线程同步中的信号量AutoResetEvent和ManualResetEvent
- C#多线程同步事件及等待句柄AutoResetEvent 和 ManualResetEvent
- 多线程之线程同步中的信号量AutoResetEvent和ManualResetEvent
- ManualResetEvent和AutoResetEvent——多线程
- C#线程控制ManualResetEvent和AutoResetEvent
- Android: Transferring Data Without Draining the Battery
- socket通信
- C# 多线程之一:信号量Semaphore
- 高富帅与大公司 续三 自我认知
- linux_linux指令_patch
- C#多线程之二:ManualResetEvent和AutoResetEvent
- 十六周任务二:成绩排序,输出最高成绩
- 关于 TCP 并发连接的几个思考题与试验
- Memento备忘录模式
- iOS学习之 plist文件的读写
- 请放慢浮躁的脚步——毕业季的感悟
- Ajax制作Web进度条
- 在IE6下form表单提交无效
- [Cocoa]深入浅出 Cocoa 之多线程 NSThread