AutoResetEvent 2
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- namespace WaitOne
- {
- class Program
- {
- static void Main(string[] args)
- {
- Calculate calc = new Calculate();
- Console.WriteLine("Result = {0}.",
- calc.Result(234).ToString());
- Console.WriteLine("Result = {0}.",
- calc.Result(55).ToString());
- }
- static void WorkMethod(object stateInfo)
- {
- Console.WriteLine("Work starting.");
- // Simulate time spent working.
- Thread.Sleep(new Random().Next(100, 2000));
- // Signal that work is finished.
- Console.WriteLine("Work ending.");
- ((AutoResetEvent)stateInfo).Set();
- }
- }
- class Calculate
- {
- double baseNumber, firstTerm, secondTerm, thirdTerm;
- AutoResetEvent[] autoEvents;
- ManualResetEvent manualEvent;
- // Generate random numbers to simulate the actual calculations.
- Random randomGenerator;
- public Calculate()
- {
- autoEvents = new AutoResetEvent[]
- {
- new AutoResetEvent(false),
- new AutoResetEvent(false),
- new AutoResetEvent(false)
- };
- manualEvent = new ManualResetEvent(false);
- }
- void CalculateBase(object stateInfo)
- {
- baseNumber = randomGenerator.NextDouble();
- Console.WriteLine("Base start");
- // Signal that baseNumber is ready.
- manualEvent.Set();
- Console.WriteLine("Base work");
- }
- // The following CalculateX methods all perform the same
- // series of steps as commented in CalculateFirstTerm.
- void CalculateFirstTerm(object stateInfo)
- {
- // Perform a precalculation.
- double preCalc = randomGenerator.NextDouble();
- Console.WriteLine("First start");
- // Wait for baseNumber to be calculated.
- manualEvent.WaitOne();
- Console.WriteLine("First work.");
- // Calculate the first term from preCalc and baseNumber.
- firstTerm = preCalc * baseNumber *
- randomGenerator.NextDouble();
- // Signal that the calculation is finished.
- autoEvents[0].Set();
- }
- void CalculateSecondTerm(object stateInfo)
- {
- double preCalc = randomGenerator.NextDouble();
- Console.WriteLine("Second Start..");
- manualEvent.WaitOne();
- Console.WriteLine("Second Work..");
- secondTerm = preCalc * baseNumber *
- randomGenerator.NextDouble();
- autoEvents[1].Set();
- }
- void CalculateThirdTerm(object stateInfo)
- {
- double preCalc = randomGenerator.NextDouble();
- Console.WriteLine("Third Start..");
- manualEvent.WaitOne();
- Console.WriteLine("Third work..");
- thirdTerm = preCalc * baseNumber *
- randomGenerator.NextDouble();
- autoEvents[2].Set();
- }
- public double Result(int seed)
- {
- randomGenerator = new Random(seed);
- // Simultaneously calculate the terms.
- ThreadPool.QueueUserWorkItem(
- new WaitCallback(CalculateBase));
- ThreadPool.QueueUserWorkItem(
- new WaitCallback(CalculateFirstTerm));
- ThreadPool.QueueUserWorkItem(
- new WaitCallback(CalculateSecondTerm));
- ThreadPool.QueueUserWorkItem(
- new WaitCallback(CalculateThirdTerm));
- // Wait for all of the terms to be calculated.
- WaitHandle.WaitAll(autoEvents);
- // Reset the wait handle for the next calculation.
- manualEvent.Reset();
- return firstTerm + secondTerm + thirdTerm;
- }
- }
- }
AutoResetEvent 2的更多相关文章
- C#各种同步方法 lock, Monitor,Mutex, Semaphore, Interlocked, ReaderWriterLock,AutoResetEvent, ManualResetEvent
看下组织结构: System.Object System.MarshalByRefObject System.Threading.WaitHandle System.Threading.Mutex S ...
- AutoResetEvent ManualResetEvent WaitOne使用注意事项
公司还用这些老家伙没办法,用了几次这俩.每次用都要重新翻一下A片. 好好的A片楞是翻译成了禅经.把这东西弄成个玄学.微软也是吃枣药丸.参考了@风中灵药的blog.写的牛逼. 还有一些公司用到的风中灵药 ...
- 多线程AutoResetEvent
我们在线程编程的时候往往会涉及到线程的通信,通过信号的接受来进行线程是否阻塞的操作. AutoResetEvent 允许线程通过发信号互相通信.通常,此通信涉及线程需要独占访问的资源. AutoRes ...
- 多线程中的锁系统(三)-WaitHandle、AutoResetEvent、ManualResetEvent
本章主要介绍下基于内核模式构造的线程同步方式,事件,信号量. 阅读目录: 理论 WaitHandle AutoResetEvent ManualResetEvent 总结 理论 Windows的线程同 ...
- C#线程同步自动重置事件——AutoResetEvent
AutoResetEvent对象用来进行线程同步操作,AutoResetEvent类继承waitHandle类. AutoResetEvent对象有终止和非终止两种状态,终止状态是线程继续执行,非终止 ...
- C#多线程同步事件及等待句柄AutoResetEvent 和 ManualResetEvent
最近捣鼓了一下多线程的同步问题,发现其实C#关于多线程同步事件处理还是很灵活,这里主要写一下,自己测试的一些代码,涉及到了AutoResetEvent 和 ManualResetEvent,当然还有也 ...
- 多线程间通信之AutoResetEvent和ManualResetEvent的原理分析
AutoResetEvent 允许线程通过发信号互相通信. 通常,当线程需要独占访问资源时使用该类. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号. 如果 AutoRe ...
- C#读写者线程(用AutoResetEvent实现同步)(转载)
C#读写者线程(用AutoResetEvent实现同步) 1. AutoResetEvent简介 通知正在等待的线程已发生事件.无法继承此类. 常用方法简介: AutoResetEvent(bool ...
- C# ManualResetEvent和AutoResetEvent 使用笔记
一.两者区别 1.ManualResetEvent 调用一次Set()后将允许恢复所有被阻塞线程.需手动在调用WaitOne()之后调用Reset()重置信号量状态为非终止,然后再次调用WaitOne ...
- [c#基础]AutoResetEvent
摘要 AutoResetEvent:msdn的描述是通知正在等待的线程已发生事件.此类不能被继承.也就是说它有那么一个时间点,会通知正在等待的线程可以做其它的事情了. AutoResetEvent 该 ...
随机推荐
- Python3下的paramiko模块
paramiko模块是基于Python实现的SSH远程安全连接,用于SSH远程执行命令.文件传输等功能. 默认Python没有,需要手动安装:pip install paramiko SSH密码认证远 ...
- html中img标签的url如何拼接变量
<img id="pic" /> <script type="text/javascript"> var url = "xxx ...
- SSH—Struts2拦截器的应用(防止未登录用户进行操作)
前言 类似于京东.淘宝这些平台,如果单纯的去浏览页面上的一些商品显示,一点问题都没有,但是当你点击商品的订单详情或者想查看一下自己的购物车,那么就会出现通过登录进去的界面,这个就是今天要说的这个拦截器 ...
- P2173 [ZJOI2012]网络
\(\color{#0066ff}{ 题目描述 }\) 有一个无向图G,每个点有个权值,每条边有一个颜色.这个无向图满足以下两个条件: 对于任意节点连出去的边中,相同颜色的边不超过两条. 图中不存在同 ...
- mysql 模糊查询 concat()
concat() 函数,是用来连接字符串. 精确查询: select * from user where name=”zhangsan” 模糊查询: select * from user where ...
- linux系统安全加固--账号相关
linux系统安全加固 一.账号相关 1.禁用或删除无用账号 减少系统无用账号,降低安全风险. 当我们的系统安装完毕后,系统默认自带了一些虚拟账户,比如bin.adm.lp.games.postfix ...
- 查看php 相关信息
PHP系统常量 __FILE__ 当前PHP程序脚本的绝对路径及文件名称 __LINE__ 存储该常量所在的行号 __FUNCTION__ 存储该常量所在的函数名称 __CLASS__ 存储该常量所在 ...
- CF C. Three displays(DP+思维)
http://codeforces.com/contest/987/problem/C 题意:给你两个n的序列要你根据第一个序列(严格单调递增的方式)在第二个序列里找3个数加起来,输出最小的一个. 思 ...
- day_09 函数及参数
1.定义:把功能封装起来,方便下次直接调用 2.语法:def 关键词开头,空格之后接函数名称和圆括号(). def 函数名(形参) 函数体 3.参数:圆括号用来接收参数.若传入多个参数,参数之间用逗号 ...
- 单元测试mock框架——jmockit实战
JMockit是google code上面的一个java单元测试mock项目,她很方便地让你对单元测试中的final类,静态方法,构造方法进行mock,功能强大.项目地址在:http://jmocki ...