1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6.  
  7. namespace WaitOne
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Calculate calc = new Calculate();
  14. Console.WriteLine("Result = {0}.",
  15. calc.Result(234).ToString());
  16. Console.WriteLine("Result = {0}.",
  17. calc.Result(55).ToString());
  18.  
  19. }
  20.  
  21. static void WorkMethod(object stateInfo)
  22. {
  23. Console.WriteLine("Work starting.");
  24.  
  25. // Simulate time spent working.
  26. Thread.Sleep(new Random().Next(100, 2000));
  27.  
  28. // Signal that work is finished.
  29. Console.WriteLine("Work ending.");
  30. ((AutoResetEvent)stateInfo).Set();
  31. }
  32.  
  33. }
  34. class Calculate
  35. {
  36. double baseNumber, firstTerm, secondTerm, thirdTerm;
  37. AutoResetEvent[] autoEvents;
  38. ManualResetEvent manualEvent;
  39.  
  40. // Generate random numbers to simulate the actual calculations.
  41. Random randomGenerator;
  42.  
  43. public Calculate()
  44. {
  45. autoEvents = new AutoResetEvent[]
  46. {
  47. new AutoResetEvent(false),
  48. new AutoResetEvent(false),
  49. new AutoResetEvent(false)
  50. };
  51.  
  52. manualEvent = new ManualResetEvent(false);
  53. }
  54.  
  55. void CalculateBase(object stateInfo)
  56. {
  57. baseNumber = randomGenerator.NextDouble();
  58.  
  59. Console.WriteLine("Base start");
  60. // Signal that baseNumber is ready.
  61. manualEvent.Set();
  62.  
  63. Console.WriteLine("Base work");
  64. }
  65.  
  66. // The following CalculateX methods all perform the same
  67. // series of steps as commented in CalculateFirstTerm.
  68.  
  69. void CalculateFirstTerm(object stateInfo)
  70. {
  71. // Perform a precalculation.
  72. double preCalc = randomGenerator.NextDouble();
  73.  
  74. Console.WriteLine("First start");
  75. // Wait for baseNumber to be calculated.
  76. manualEvent.WaitOne();
  77.  
  78. Console.WriteLine("First work.");
  79. // Calculate the first term from preCalc and baseNumber.
  80. firstTerm = preCalc * baseNumber *
  81. randomGenerator.NextDouble();
  82.  
  83. // Signal that the calculation is finished.
  84. autoEvents[0].Set();
  85. }
  86.  
  87. void CalculateSecondTerm(object stateInfo)
  88. {
  89. double preCalc = randomGenerator.NextDouble();
  90. Console.WriteLine("Second Start..");
  91. manualEvent.WaitOne();
  92. Console.WriteLine("Second Work..");
  93. secondTerm = preCalc * baseNumber *
  94. randomGenerator.NextDouble();
  95. autoEvents[1].Set();
  96. }
  97.  
  98. void CalculateThirdTerm(object stateInfo)
  99. {
  100. double preCalc = randomGenerator.NextDouble();
  101. Console.WriteLine("Third Start..");
  102. manualEvent.WaitOne();
  103.  
  104. Console.WriteLine("Third work..");
  105. thirdTerm = preCalc * baseNumber *
  106. randomGenerator.NextDouble();
  107. autoEvents[2].Set();
  108. }
  109.  
  110. public double Result(int seed)
  111. {
  112. randomGenerator = new Random(seed);
  113.  
  114. // Simultaneously calculate the terms.
  115. ThreadPool.QueueUserWorkItem(
  116. new WaitCallback(CalculateBase));
  117. ThreadPool.QueueUserWorkItem(
  118. new WaitCallback(CalculateFirstTerm));
  119. ThreadPool.QueueUserWorkItem(
  120. new WaitCallback(CalculateSecondTerm));
  121. ThreadPool.QueueUserWorkItem(
  122. new WaitCallback(CalculateThirdTerm));
  123.  
  124. // Wait for all of the terms to be calculated.
  125. WaitHandle.WaitAll(autoEvents);
  126.  
  127. // Reset the wait handle for the next calculation.
  128. manualEvent.Reset();
  129.  
  130. return firstTerm + secondTerm + thirdTerm;
  131. }
  132. }
  133. }

  

AutoResetEvent 2的更多相关文章

  1. C#各种同步方法 lock, Monitor,Mutex, Semaphore, Interlocked, ReaderWriterLock,AutoResetEvent, ManualResetEvent

    看下组织结构: System.Object System.MarshalByRefObject System.Threading.WaitHandle System.Threading.Mutex S ...

  2. AutoResetEvent ManualResetEvent WaitOne使用注意事项

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

  3. 多线程AutoResetEvent

    我们在线程编程的时候往往会涉及到线程的通信,通过信号的接受来进行线程是否阻塞的操作. AutoResetEvent 允许线程通过发信号互相通信.通常,此通信涉及线程需要独占访问的资源. AutoRes ...

  4. 多线程中的锁系统(三)-WaitHandle、AutoResetEvent、ManualResetEvent

    本章主要介绍下基于内核模式构造的线程同步方式,事件,信号量. 阅读目录: 理论 WaitHandle AutoResetEvent ManualResetEvent 总结 理论 Windows的线程同 ...

  5. C#线程同步自动重置事件——AutoResetEvent

    AutoResetEvent对象用来进行线程同步操作,AutoResetEvent类继承waitHandle类. AutoResetEvent对象有终止和非终止两种状态,终止状态是线程继续执行,非终止 ...

  6. C#多线程同步事件及等待句柄AutoResetEvent 和 ManualResetEvent

    最近捣鼓了一下多线程的同步问题,发现其实C#关于多线程同步事件处理还是很灵活,这里主要写一下,自己测试的一些代码,涉及到了AutoResetEvent 和 ManualResetEvent,当然还有也 ...

  7. 多线程间通信之AutoResetEvent和ManualResetEvent的原理分析

    AutoResetEvent 允许线程通过发信号互相通信. 通常,当线程需要独占访问资源时使用该类. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号. 如果 AutoRe ...

  8. C#读写者线程(用AutoResetEvent实现同步)(转载)

    C#读写者线程(用AutoResetEvent实现同步) 1. AutoResetEvent简介 通知正在等待的线程已发生事件.无法继承此类. 常用方法简介: AutoResetEvent(bool ...

  9. C# ManualResetEvent和AutoResetEvent 使用笔记

    一.两者区别 1.ManualResetEvent 调用一次Set()后将允许恢复所有被阻塞线程.需手动在调用WaitOne()之后调用Reset()重置信号量状态为非终止,然后再次调用WaitOne ...

  10. [c#基础]AutoResetEvent

    摘要 AutoResetEvent:msdn的描述是通知正在等待的线程已发生事件.此类不能被继承.也就是说它有那么一个时间点,会通知正在等待的线程可以做其它的事情了. AutoResetEvent 该 ...

随机推荐

  1. Python3下的paramiko模块

    paramiko模块是基于Python实现的SSH远程安全连接,用于SSH远程执行命令.文件传输等功能. 默认Python没有,需要手动安装:pip install paramiko SSH密码认证远 ...

  2. html中img标签的url如何拼接变量

    <img id="pic" /> <script type="text/javascript"> var url = "xxx ...

  3. SSH—Struts2拦截器的应用(防止未登录用户进行操作)

    前言 类似于京东.淘宝这些平台,如果单纯的去浏览页面上的一些商品显示,一点问题都没有,但是当你点击商品的订单详情或者想查看一下自己的购物车,那么就会出现通过登录进去的界面,这个就是今天要说的这个拦截器 ...

  4. P2173 [ZJOI2012]网络

    \(\color{#0066ff}{ 题目描述 }\) 有一个无向图G,每个点有个权值,每条边有一个颜色.这个无向图满足以下两个条件: 对于任意节点连出去的边中,相同颜色的边不超过两条. 图中不存在同 ...

  5. mysql 模糊查询 concat()

    concat() 函数,是用来连接字符串. 精确查询: select * from user where name=”zhangsan” 模糊查询: select * from user where ...

  6. linux系统安全加固--账号相关

    linux系统安全加固 一.账号相关 1.禁用或删除无用账号 减少系统无用账号,降低安全风险. 当我们的系统安装完毕后,系统默认自带了一些虚拟账户,比如bin.adm.lp.games.postfix ...

  7. 查看php 相关信息

    PHP系统常量 __FILE__ 当前PHP程序脚本的绝对路径及文件名称 __LINE__ 存储该常量所在的行号 __FUNCTION__ 存储该常量所在的函数名称 __CLASS__ 存储该常量所在 ...

  8. CF C. Three displays(DP+思维)

    http://codeforces.com/contest/987/problem/C 题意:给你两个n的序列要你根据第一个序列(严格单调递增的方式)在第二个序列里找3个数加起来,输出最小的一个. 思 ...

  9. day_09 函数及参数

    1.定义:把功能封装起来,方便下次直接调用 2.语法:def 关键词开头,空格之后接函数名称和圆括号(). def 函数名(形参) 函数体 3.参数:圆括号用来接收参数.若传入多个参数,参数之间用逗号 ...

  10. 单元测试mock框架——jmockit实战

    JMockit是google code上面的一个java单元测试mock项目,她很方便地让你对单元测试中的final类,静态方法,构造方法进行mock,功能强大.项目地址在:http://jmocki ...