C#多线程之二:ManualResetEvent和AutoResetEvent
来源:互联网 发布:淘宝顶呱呱代练lol 编辑:程序博客网 时间:2024/05/21 06:49
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();
}
}
}
运行结果:
- 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
- 算法训练 字符串逆序
- 炮兵阵地 POJ
- 两两交换链表中的节点
- 全栈(FullStack)工程师指南
- emWin 2天速成实例教程003_Framewin和Window窗口控件和2D绘图
- C#多线程之二:ManualResetEvent和AutoResetEvent
- linux内核中断
- 汇编中将代码,数据,栈放入相同段空间的缺点
- 线程间的通信、同步方式与进程间通信方式
- leetcode有序数组转换成BST
- 用迅雷下载好的win7系统怎么安装?
- 深入浅出数据仓库中SQL性能优化之Hive篇
- web服务器、容器和中间件
- SpringMVC的各种参数绑定方式