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;
}
}
}

  

最新文章

  1. CatchPacket网络抓包软件
  2. QM UML状态机建模实例之移植 cortex-m0
  3. ViewHolder被设计成静态内部类的作用
  4. Insert Function before and after main function
  5. Linux下面如何安装Django
  6. Qt之QTableView显示富文本
  7. CD key 生成
  8. [转]TOMCAT原理以及处理HTTP请求的过程、ContextPath ServletPath
  9. Hadoop的RPC通信原理
  10. 在UltraEdit中如何像NotePad++一样实现双击单词在全文中高亮
  11. Android开发:Android Studio开发环境配置
  12. JavaScript字符串相关
  13. ueditor 编辑器常用方法
  14. java 几种对象
  15. Kubernetes+Prometheus+Grafana部署笔记
  16. mongodb chunk 大小设置
  17. Using XmlHttpRequest 写JSON网页
  18. 【51nod】1564 区间的价值
  19. java基础-四大特征
  20. ESP8266调试笔记

热门文章

  1. Object detection with deep learning and OpenCV
  2. ubuntu部署django详细教程
  3. hadoop HDFS文件系统的特征
  4. 资深专家深度剖析Kubernetes API Server第2章(共3章)
  5. UESTC 趣味赛命题报告E
  6. POJ1059 Chutes and Ladders
  7. Python字符串拼接、格式化输出、深浅复制
  8. 2015苏州大学ACM-ICPC集训队选拔赛(1) 1001 1002 1010
  9. jenkins出现的问题
  10. esper(2)-事件类型