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的更多相关文章

  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. git 本地分支与远程分支相关操作记录

    1.远程分支中有新增分支,但自己的本地分支没有对应同步 git checkout -b [remote-branch-name] origin/[remote-branch-name] 2. 查看本地 ...

  2. Linux安装与分区解释

    Linux安装过程中最重要的就是对硬盘进行分区: Linux是先建立一个根目录“/”,然后在根目录上建立一系列的空目录,接着把硬盘分区挂载到相应目录上. 在linux系统中至少必须有两个挂载点(磁盘分 ...

  3. 牛客Professional Manager(并查集)

    t’s universally acknowledged that there’re innumerable trees in the campus of HUST.  Thus a professi ...

  4. 对bootstrap模态框的小尝试

    bootstrap中有一个“模态框”插件,我理解的意思就是一个具有全局遮罩的弹窗提示,官方解释是:模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开父窗 ...

  5. Java的JsonHelper

    <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <g ...

  6. javaEE 转发 和 重定向

    转发 两种方法: 1,request.getRequestDispatcher(getServletContext().getContextPath() + "/path").fo ...

  7. Python 基础学习之if语句

    cars=['audi','xiali','bwm','benz',] ##根据要求bmw全部为大写,其他的首字母为大写 for car in cars: if car=='bmw': print(c ...

  8. 基于spring boot 和MDC实现 同一笔记录的日志跟踪实现--1.filter

    同一个项目中,一般包含controller/servlet.service.dao等.1笔记录的日志贯穿于controller.service.dao中,在并发情况下,那如何找出该笔日志? 可通过以下 ...

  9. c++从txt中读取数据,数据并不是一行路径(实用)

    #include <iostream>#include <fstream>#include <string> using namespace std; //输出空行 ...

  10. 如何 将下载离线 nupkg 文件 安装到VS2017

      https://www.cnblogs.com/cncc/articles/8276878.html   --------------------------------------------- ...