WaitHandle、AutoResetEvent、ManualResetEvent整理
来源:互联网 发布:模拟法庭教学软件 编辑:程序博客网 时间:2024/05/18 01:26
WaitHandle是一个用以控制线程同步的类,有WaitOne()方法,用以阻止当前线程(提前是其状态为Nonsignaled,即红灯,后面解释),直到当前的WaitHandle 收到绿灯信号。
WaitHandle有两个状态"Signaled" and "Nonsignaled",前者可以理解为绿灯,绿灯状态时WaitOne方法无效,当前线程不会被阻止,后者可以理解为红灯,WaitOne()方法会阻止当前线程。
这两个状态是通过函数Set()和Reset()来设置的。前者设为绿灯,后者设为红灯。
(1):一般来说,初始状态都是"Nonsignaled",即红灯,这样当WaitOne时,线程会暂停掉,这正是通常情况下我们调用WaitOne时想要的效果。这也正是Reset(字面义“重置”)被定义为设成红灯的原因。所以初始化时参数用false,代表初始状态为红灯。
ManualResetEvent manualWait = new ManualResetEvent(false);
(2): WaitHandle没有Set方法,所以不能使暂停的线程得以继续,所以一般不单用WaitHandle。其常会用作参数,尤其是AutoResetEvent和ManualResetEvent都用到的情况,提供一定的通用性。
AutoResetEvent、ManualResetEvent都继承自WaitHandle,都有Set()和Reset()方法,当然也继承了WaitOne()方法和两个状态。
(1)ManualResetEvent的Set()和Reset()方法即上面的功能,将状态分别设成绿灯和红灯。
(2)AutoResetEvent比较特别,正如名字所示,在Set()完后,它会立即自动调用Reset,所以一般只用Set()方法。
说明:这类对象可以被用在多个并发线程中,只要调用WaitOne,则都会暂停运行。ManualResetEvent的Set,可以让所有这些线程都继续运行,Reset又都停止,而AutoResetEvent则通过Set可实现一次只有一个被阻止的线程释放。
- WaitHandle、AutoResetEvent、ManualResetEvent整理
- 多线程中的锁系统(三)-WaitHandle、AutoResetEvent、ManualResetEvent
- AutoResetEvent&&ManualResetEvent
- 基元线程同步——内核模式构造(WaitHandle,EventWaitHandle,AutoResetEvent,ManualResetEvent,Semaphore,Mutex)
- WaitHandle——使用ManualResetEvent
- AutoResetEvent 与 ManualResetEvent区别
- AutoResetEvent与ManualResetEvent区别
- AutoResetEvent 与 ManualResetEvent
- AutoResetEvent与ManualResetEvent区别
- AutoResetEvent与ManualResetEvent
- AutoResetEvent与ManualResetEvent区别
- AutoResetEvent和ManualResetEvent
- AutoResetEvent 与 ManualResetEvent
- AutoResetEvent与ManualResetEvent区别
- AutoResetEvent和ManualResetEvent
- AutoResetEvent与ManualResetEvent区别
- AutoResetEvent和ManualResetEvent
- ManualResetEvent,AutoResetEvent 学习
- JQuery获取checkbox的选中状态
- 关于类basic_ostream的构造函数被申明成protected
- myeclipse安装svn插件的多种方式
- java IO
- porting gps to android2.3
- WaitHandle、AutoResetEvent、ManualResetEvent整理
- RAC环境JDBC连接池
- js 判断变量是否 字符串变量
- tomcat下的ssl双向认证
- 仙静发布QQ空间日志_王子依旧伤感,依旧寂寞
- Linux系统,使用C语言如何产生UUID
- “郭德纲”挤兑人不带脏字
- Excel2007 制作饼图
- Hi-Z(高阻态)