C# AutoResetEvent的使用
来源:才华咖 本文已影响2.38W人
来源:才华咖 本文已影响2.38W人
AutoResetEvent 允许线程通过发信号互相通信。通常,此通信涉及线程需要独占访问的资源。线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号。如果 AutoResetEvent 处于非终止状态,则该线程阻塞,并等待当前控制资源的线程
通过调用 Set 发出资源可用的信号。
调用 Set 向 AutoResetEvent 发信号以释放等待线程。AutoResetEvent 将保持终止状态,直到一个正在等待的线程被释放,然后自动返回非终止状态。如果没有任何线程在等待,则状态将无限期地保持为终止状态。
可以通过将一个布尔值传递给构造函数来控制 AutoResetEvent 的初始状态,如果初始状态为终止状态,则为 true;否则为 false。
通俗的来讲只有等()成功运行后,One()才能够获得运行机会;Set是发信号,WaitOne是等待信号,只有发了信号,
等待的才会执行。如果不发的话,WaitOne后面的'程序就永远不会执行。下面我们来举一个例子:我去书店买书,当我选中一本书后我会去收费处付钱,
付好钱后再去仓库取书。这个顺序不能颠倒,我作为主线程,收费处和仓库做两个辅助线程,代码如下:
using System;
using ;
using vities;
using ements;
using ading;
namespace CaryAREDemo
{
class Me
{
const int numIterations = 550;
static AutoResetEvent myResetEvent = new AutoResetEvent(false);
static AutoResetEvent ChangeEvent = new AutoResetEvent(false);
//static ManualResetEvent myResetEvent = new ManualResetEvent(false);
//static ManualResetEvent ChangeEvent = new ManualResetEvent(false);
static int number; //这是关键资源
static void Main()
{
Thread payMoneyThread = new Thread(new ThreadStart(PayMoneyProc));
= "付钱线程";
Thread getBookThread = new Thread(new ThreadStart(GetBookProc));
= "取书线程";
t();
t();
for (int i = 1; i <= numIterations; i++)
{
eLine("买书线程:数量{0}", i);
number = i;
//Signal that a value has been written.
();
();
p(0);
}
t();
t();
}
static void PayMoneyProc()
{
while (true)
{
One();
//t();
eLine("{0}:数量{1}", , number);
}
}
static void GetBookProc()
{
while (true)
{
One();
// t();
eLine("{0}:数量{1}", , number);
eLine("------------------------------------------");
p(0);
}
}
}
}
运行结果如下:
AutoResetEvent与ManualResetEvent的区别
他们的用法声明都很类似,Set方法将信号置为发送状态 Reset方法将信号置为不发送状态WaitOne等待信号的发送。其实,从名字就可以看出一个手动,
一个自动,这个手动和自动实际指的是在Reset方法的处理上,如下面例子:
public AutoResetEvent autoevent=new AutoResetEvent(true);
public ManualResetEvent manualevent=new ManualResetEvent(true);
默认信号都处于发送状态,
One();
One();
如果 某个线程调用上面该方法,则当信号处于发送状态时,该线程会得到信号,得以继续执行。差别就在调用后,One()每次只允许一个线程
进入,当某个线程得到信号(也就是有其他线程调用了()方法后)后,autoevent会自动又将信号置为不发送状态,则其他调用WaitOne的线程只
有继续等待.也就是说,autoevent一次只唤醒一个线程。而manualevent则可以唤醒多个线程,因为当某个线程调用了set方法后,其他调用waitone的线程
获得信号得以继续执行,而manualevent不会自动将信号置为不发送.也就是说,除非手工调用了t().方法,则manualevent将一直保持有信号状态,manualevent也就可以同时唤醒多个线程继续执行。如果上面的程序换成ManualResetEvent的话,就需要在waitone后面做下reset。
托福英语写作范文:Examinations exert a pernicious influence on educat
It was nice to see your letter为题目的作文
Efforts Urged to Improve Womens Reproductive Health英语阅读
Covering the ears to steal the bell高中英语作文
初一英语Unit 12 My favorite subject is science 课后练习题答
初三年级英语课文复习 Unit 1. Try not to translate every word
英语满分作文:Reading-TheBestMeanstoAcquireknowledge
八年级下册Unit 9 have you ever been to museum? 说课稿
Java中break、continue、return语句的使用区别对比
Java中break、continue、return语句的使用区别对比介绍
TV Receivers in Students Flats大学英语作文
Its Our Duty To Protect Our Environment-作文及点评
六级英语写作范文:The Celebration of Western Festivals
Unit 9 A Have you ever been to a museum?八年级英语说课稿
大学生面临许多压力The Pressures on Collese Students英语作文
Ought Parents to Give Children Pocket英语作文
英语优秀作文:M Unfrgettable Experiences
关于javascript中encodeURI和decodeURI的使用方法
C++中const-cast与reinterpret-cast运算符的用法
新概念英语青少版2a课文Unit 04:The top three persent
优秀英语作文My Experience to Eat Buffet
儿童英语作文Teenager’s Education
Unit 2 Turn left at the corner教学反思
VOA的慢速英语听力文摘:Mystery at The Ice Castle Inn
《The Prevalence of Western Holidays》英语四级作文
腾讯c/c++面试用的笔试题
心理c证的培训心得简短 心理C证心得体会通用多篇
Five Sentences to Hurt Children英语作文
英语作文:An unforgettable experience
c开头c结尾的单词
关于c罗的读后感通用7篇
Unforgettable experience 难忘的一件事作文
以Travels for the environment 为例的教学设计
The relationship between theory and practice大学英语作文
高中优秀英语作文An Unforgettable Experience
java如何利用java.net.URLConnection发送HTTP请求
六年级英语上册Unit2 New York is in the east of America说课稿
英语阅读:Heat Is on These Countries for the Heat
A Letter to the Editor of a Newspaper英语四级作文
公共英语二级写作Preserving Natural Resources