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 该 ...
随机推荐
- 关于nohup命令
nohup java -jar Test.jar --server.port=443 > console.log 2>&1 & 关于nohup命令 when using t ...
- 跨平台技术iOS与安卓
1.教学资源获取 Flutter的使用教学笔记 2.本地学习笔记
- react 部分语法补充
jsx语法 todolist.js import React, { Component,Fragment } from 'react'; import './style.css' class Todo ...
- C#后端调用WebApi地址
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using Syst ...
- visual studio 2013 error: Page '312e8a59-2712-48a1-863e-0ef4e67961fc' not found.
In order to resolve this error do the following : Open Developer Command Prompt for VS 2013 as “Run ...
- Luogu P1503 鬼子进村 set
还是拿set搞... 用set记录每个被摧毁的位置,Q的时候二分一下,在和上一个摧毁的位置减一下,即可求出能到的房子数 #include<iostream> #include<cst ...
- poj1860 兑换货币(bellman ford判断正环)
传送门:点击打开链接 题目大意:一个城市有n种货币,m个货币交换点,你有v的钱,每个交换点只能交换两种货币,(A换B或者B换A),每一次交换都有独特的汇率和手续费,问你存不存在一种换法使原来的钱更多. ...
- OJ 26217 :Work Scheduling(贪心+优先队列)
约翰有太多的工作要做.为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间. 他的工作日从0时刻开始,有10^8个单位时间.在任一时刻,他都可以选择编号1~N的N(1 <= N &l ...
- Life is a journey
Life is a journey. What we should care about is not where it's headed but what we see and how we fee ...
- 机器学习值KNN