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的基本用法的更多相关文章

  1. AutoResetEvent控制线程用法

    本文主要来自一道面试题,由于之前对AutoResetEvent的用户很模糊(即使已经使用过了).面试题题目很简洁:两个线程交替打印0~100的奇偶数.你可以先动手试试,我主要是尝试在一个方法里面完成这 ...

  2. C# 死锁 Task/AutoResetEvent

    与之前<C# 死锁 TaskCompletionSource>类似,还有很多死锁的案例 使用Task异步转同步时,使用不当造成的死锁 private void Task_OnClick(o ...

  3. C#多线程的用法8-线程间的协作AutoResetEvent

    AutoResetEvent自动重置事件,与ManualResetEvent是相对的而言.它同样用于线程间同步,请对照<C#多线程的用法7-线程间的协作ManualResetEvent>进 ...

  4. 一个实例明白AutoResetEvent和 ManulResetEvent的用法

    先看一段代码: public class WaitHandlerExample {                 public static AutoResetEvent waitHandler;  ...

  5. 浅谈AutoResetEvent的用法

    using System;using System.Threading; namespace AutoResetEvent_Examples{    class MyMainClass    {    ...

  6. AutoResetEvent ManualResetEvent WaitOne使用注意事项

    公司还用这些老家伙没办法,用了几次这俩.每次用都要重新翻一下A片. 好好的A片楞是翻译成了禅经.把这东西弄成个玄学.微软也是吃枣药丸.参考了@风中灵药的blog.写的牛逼. 还有一些公司用到的风中灵药 ...

  7. System.Threading.Timer 定时器的用法

    System.Threading.Timer 是C# 中的一个定时器,可以定时(不断循环)执行一个任务.它是在线程上执行的,具有很好的安全性.为此  .Net Framework 提供了5个重载的构造 ...

  8. [转]AutoResetEvent 与 ManualResetEvent区别

    在C#多线程编程中,这两个类几乎是不可或缺的,他们的用法/声明都很类似,那么区别在哪里了? Set方法将信号置为发送状态 Reset方法将信号置为不发送状态 WaitOne等待信号的发送 其实,从名字 ...

  9. C#多线程之二:ManualResetEvent和AutoResetEvent

    初次体验 ManualResetEvent和AutoResetEvent主要负责多线程编程中的线程同步:以下一段是引述网上和MSDN的解析: 在.Net多线程编程中,AutoResetEvent和Ma ...

随机推荐

  1. 解决WinScp连接被拒绝的问题

    尝试以下方法: 1) 开启|关闭防火墙(这里需要关闭) sudo ufw enable|disable 2) 开启远程服务 在终端界面输入:service sshd start.结果显示:ssh:un ...

  2. mybatis中的resultMap实际作用

    resultMap和resultType在实际的使用上完全可以进行替换,但是resultMap有比resultType更多的一个功能.我们先定义一个简单的resultMap例子 <resultM ...

  3. Java基础之进制转换

    1.十进制与二进制之间的转换 (1)十进制转二进制的方法:使用十进制的数据不断除以2,直到商为0为止,从下往上取余就是对应的二进制. (2)二进制转十进制:使用二进制的每一位乘以2的n次方,n从0开始 ...

  4. linux 网络编程 3---(io多路复用,tcp并发)

    1,io模型: 阻塞io.非阻塞io.io多路复用,信号驱动io. 阻塞Io与非阻塞io的转换,可用fcntl()函数 #include<unistd.h> #include<fcn ...

  5. Java String 字符串类细节探秘

    一. 字符串基本知识要点 字符串类型String是Java中最常用的引用类型.我们在使用Java字符串的时候,通常会采用两种初始化的方式:1. String str = "Hello Wor ...

  6. MySQL 取得字段子串修改

    MySQL 中, GeneID 为 GRMZM2G549533_P01,1123,45 , 需要修改为 GRMZM2G549533_P01 update test set GeneID=SUBSTRI ...

  7. python3基础盲点

    数值类型 Python支持四种不同的数值类型,包括int(整数)long(长整数)float(浮点数)complex (复数) python3对整数的大小不做限制 算数运算符 优先级: 逻辑运算符 优 ...

  8. Jmeter登录接口返回 status415

    1.现象:在查看结果树中看到: Request Headers:Connection: keep-aliveContent-Type: application/x-www-form-urlencode ...

  9. ADB连接不上手机,端口5037被占用的情况解决

    最近在搞手机APP自动化测试,adb连接手机时提示端口被占用 检测5037端口被谁占用,cmd窗口输入命令:netstat -ano | findstr "5037" (注意”50 ...

  10. 七 Appium常用方法介绍

    文本转自:http://www.cnblogs.com/sundalian/p/5629609.html 由于appium是扩展了Webdriver协议,所以可以使用webdriver提供的方法,比如 ...