C# AutoResetEvent的使用
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。
相關文章
-
A Letter to Univsity Pesident about the Canteen Service on C
dear president:i am a student who lives on campus and uses canteen services everyday. i would love to share my opinion on the services i received. also, i have some suggestions on how to improve the c -
A Letter to the University President为题目的作文
Direction: For this part, you are allowed thirty minutes to write a composition on the topic: A letter to the university president about the canteen service on campus, you should write at least 100 words, -
The Best Thing That Could Ever Happen少儿双语阅读
Unit 3 The Best Thing That Could Ever Happen单元 3 最棒的事Three old men were sitting around and talking.三个老头坐在一起聊天。The 80-year-old remarked, "The best thing that could happen to me would j -
四级作文:Create a Conservation-Oriented Campus
四级作文:Create a Conservation-Oriented Campus,四级作文:Create a Conservation-Oriented Campus范文,免费提供给大家交流参考! Create a Conservation-Oriented Campus1.建设节约型校园十分重要2.原因是…&he -
托福英语写作范文:Examinations exert a pernicious influence on educat
We might marvel at the progress made in every field of study, but the methods of testing a person’s knowledge and ability remain as primitive as ever they were. It really is extraordinary that after -
It was nice to see your letter为题目的作文
Dear John,It was nice to see your letter. Since you have been having some problems recently, I will give you some advice. First you should buy some new clothes. You could borrow some money from your p -
九年级年级英语课文复习 Unit 1. Try not to translate every word
1. try(not) to do sth.尽力(不)做某事:Try to lea English well. 尽量学好英语。Try not to translate every word. 尽量不要翻译每个单词。2. write down写下,记下:Please write down your mistakes in your notebooks. -
Unit 9 A Have you ever been to a museum?八年级英语说课稿
在教学工作者开展教学活动前,总归要编写说课稿,写说课稿能有效帮助我们总结和提升讲课技巧。如何把说课稿做到重点突出呢?下面是小编为大家收集的Unit 9 A Have you ever been to a museum?八年级英语说课稿,供大家参考借 -
八年级下册Unit 9 have you ever been to museum? 说课稿
我说课的内容是人教版义务教育新目标Go for it八年级下册第9单元Have you ever been to a museum? Section A的3a部分。下面我将从资源分析、学法指导、教材教法和教学设计四个方面简述我对本节课的理解与处理。 一 -
Efforts Urged to Improve Womens Reproductive Health英语阅读
Efforts Urged to Improve Women's Reproductive HealthParliamentarians from over 100 countries are being urged to take action in improving women's reproductive health ahead of the 2015 deadline