AutoResetEvent的基本用法
The following example uses an AutoResetEvent to synchronize the activities of two threads.The first thread, which is the application thread, executes Main.It writes values to the protected resource, which is a static (Shared in Visual Basic) field named number.The second thread executes the static ThreadProc method, which reads the values written by Main.
The ThreadProc method waits for the AutoResetEvent.When Main calls the Set method on the AutoResetEvent, the ThreadProc method reads one value.The AutoResetEvent immediately resets, so the ThreadProc method waits again.
The program logic guarantees that the ThreadProc method will never read the same value two times.It does not guarantee that theThreadProc method will read every value written by Main.That guarantee would require a second AutoResetEvent lock.
After each write operation, Main yields by calling the Thread.Sleep method, to give the second thread a chance to execute.Otherwise, on a single-processor computer Main would write many values between any two read operations.
class Program
{
//Initially not signaled.
private const int NumIterations = ; private static readonly AutoResetEvent MyResetEvent = new AutoResetEvent(false); private static int _number; public static void Main()
{
//Create and start the reader thread.
var myReaderThread = new Thread(new ThreadStart(MyReadThreadProc));
myReaderThread.Name = "ReaderThread";
myReaderThread.Start(); for (int i = ; i <= NumIterations; i++)
{
Console.WriteLine("Writer thread writing value: {0}", i);
_number = i; //Signal that a value has been written.
MyResetEvent.Set(); //Give the Reader thread an opportunity to act.
Thread.Sleep();
} //Terminate the reader thread.
myReaderThread.Abort(); Console.ReadKey();
} private static void MyReadThreadProc()
{
while (true)
{
//The value will not be read until the writer has written
// at least once since the last read.
MyResetEvent.WaitOne();
Console.WriteLine("{0} reading value: {1}", Thread.CurrentThread.Name, _number);
}
}
}
AutoResetEvent的基本用法的更多相关文章
- AutoResetEvent控制线程用法
本文主要来自一道面试题,由于之前对AutoResetEvent的用户很模糊(即使已经使用过了).面试题题目很简洁:两个线程交替打印0~100的奇偶数.你可以先动手试试,我主要是尝试在一个方法里面完成这 ...
- C# 死锁 Task/AutoResetEvent
与之前<C# 死锁 TaskCompletionSource>类似,还有很多死锁的案例 使用Task异步转同步时,使用不当造成的死锁 private void Task_OnClick(o ...
- C#多线程的用法8-线程间的协作AutoResetEvent
AutoResetEvent自动重置事件,与ManualResetEvent是相对的而言.它同样用于线程间同步,请对照<C#多线程的用法7-线程间的协作ManualResetEvent>进 ...
- 一个实例明白AutoResetEvent和 ManulResetEvent的用法
先看一段代码: public class WaitHandlerExample { public static AutoResetEvent waitHandler; ...
- 浅谈AutoResetEvent的用法
using System;using System.Threading; namespace AutoResetEvent_Examples{ class MyMainClass { ...
- AutoResetEvent ManualResetEvent WaitOne使用注意事项
公司还用这些老家伙没办法,用了几次这俩.每次用都要重新翻一下A片. 好好的A片楞是翻译成了禅经.把这东西弄成个玄学.微软也是吃枣药丸.参考了@风中灵药的blog.写的牛逼. 还有一些公司用到的风中灵药 ...
- System.Threading.Timer 定时器的用法
System.Threading.Timer 是C# 中的一个定时器,可以定时(不断循环)执行一个任务.它是在线程上执行的,具有很好的安全性.为此 .Net Framework 提供了5个重载的构造 ...
- [转]AutoResetEvent 与 ManualResetEvent区别
在C#多线程编程中,这两个类几乎是不可或缺的,他们的用法/声明都很类似,那么区别在哪里了? Set方法将信号置为发送状态 Reset方法将信号置为不发送状态 WaitOne等待信号的发送 其实,从名字 ...
- C#多线程之二:ManualResetEvent和AutoResetEvent
初次体验 ManualResetEvent和AutoResetEvent主要负责多线程编程中的线程同步:以下一段是引述网上和MSDN的解析: 在.Net多线程编程中,AutoResetEvent和Ma ...
随机推荐
- python 自定义函数表达式 拟合求系数
https://docs.scipy.org/doc/scipy/reference/tutorial/integrate.html https://docs.scipy.org/doc/scipy/ ...
- ubantu忘记登录密码怎么办?(ubantu16.04)
刚知道有虚拟机的时候为了那啥(咳咳),花了好几天的时间装了很多的操作系统,像什么os x.kali.red hat.以前的win7什么的,甚至还有一个Android的虚拟机……扯偏了,然后最近学习到了 ...
- 观看杨老师(杨旭)Asp.Net Core MVC入门教程记录
观看杨老师(杨旭)Asp.Net Core MVC入门教程记录 ASP.NET Core MVC入门 Asp.Net Core启动和配置 Program类,Main方法 Startup类 依赖注入,I ...
- SQL条件判断中字符串后面有空格的问题
也不知何时才有的概念,还是以前一直没有注意,从哪也没有听说过的定义,今天又遇见了一个小坑,特记录下来,防止再陷坑! 才疏学浅,文笔有限,简单点说吧,就是在写SQL Server语句时,以前使用了 WH ...
- 从官网下载centos
今天想从官网下载6.5版本的CentOS,结果找了好一会儿才找到,赶紧记录下来,以备以后查询. 第一步在百度搜索centos,点击"Download CentOS",如下图所示. ...
- 北京Uber优步司机奖励政策(3月11日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Java:break和continue关键字的作用
二者的作用和区别 1. break:直接跳出当前循环体(while.for.do while)或程序块(switch).其中switch case执行时,一定会先进行匹配,匹配成功返回当前case的值 ...
- underscore.js 分析 第二天
Underscore源码中有这么句obj.length === +obj.length意思是typeof obj.length == number,即检测obj的长度是否是数字我的理解:这么写是来检测 ...
- SpringBoot学习:整合shiro(rememberMe记住我后自动登录session失效解决办法)
项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 定义一个拦截器,判断用户是通过记住我登录时,查询数据库后台自动登录,同时把用户放入ses ...
- 第六模块:WEB框架开发 第1章·Django框架开发1~50
01-Django基础介绍 02-Web应用程序1 03-Web应用程序2 04-http请求协议1 05-http请求协议2 06-http协议之响应协议 07-wsgire模块1 08-wsgir ...