ManualResetEvent & AutoResetEvent
参考资料:
1. https://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx
2. https://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx
二者的区别,跑一下下边的程序就懂了。
ManualResetEvent:通知一个或多个等待线程该事件已经发生。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace Test01
{
class Program
{
private static ManualResetEvent mre = new ManualResetEvent(false); static void Main(string[] args)
{
for (int i = ; i < ; i++)
{
Thread thread = new Thread(ThreadProc);
thread.Name = "Thread_" + i;
thread.Start();
} Thread.Sleep();
Console.WriteLine("If you want to release all threads. Please press 'Enter'.");
Console.ReadLine(); mre.Set();
Console.WriteLine("All threads have been released.");
mre.Reset();
} private static void ThreadProc()
{
string threadName = Thread.CurrentThread.Name; Console.WriteLine("Thread: " + threadName + " starts and begin WaitOne().");
mre.WaitOne();
Console.WriteLine("Thread: " + threadName + " gets the signal and ends.");
}
}
}
ManualResetEvent
AutoResetEvent:通知一个等待线程该事件已经发生。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace Test02
{
class Program
{
private static AutoResetEvent are = new AutoResetEvent(false); static void Main(string[] args)
{
for (int i = ; i < ; i++)
{
Thread thread = new Thread(ThreadProc);
thread.Name = "Thread_" + i;
thread.Start();
} Thread.Sleep(); Console.WriteLine("Please press Enter to release the signal.");
Console.ReadLine();
are.Set();
} private static void ThreadProc()
{
string threadName = Thread.CurrentThread.Name; Console.WriteLine("Thread: " + threadName + " waits for AutoResetEvent.");
are.WaitOne();
Console.WriteLine("Thread: " + threadName + " gets the signal and is released.");
are.Set();
}
}
}
AutoResetEvent_1
ManualResetEvent & AutoResetEvent的更多相关文章
- 重新想象 Windows 8 Store Apps (47) - 多线程之线程同步: Semaphore, CountdownEvent, Barrier, ManualResetEvent, AutoResetEvent
[源码下载] 重新想象 Windows 8 Store Apps (47) - 多线程之线程同步: Semaphore, CountdownEvent, Barrier, ManualResetEve ...
- ManualResetEvent,AutoResetEvent说明
相信不少人对ManualResetEvent,AutoResetEvent的状态比较晕,下面是本人认为最精简的理解 1.只有2种状态,终止态 And 非终止态 终止状态,既然是状态那么一定对应事物,这 ...
- AutoResetEvent和ManualResetEvent理解 z
AutoResetEvent和ManualResetEvent用于多线程之间代码执行顺序的控制,它们继承自WaitHandle,API相同,但在使用中还是有区别的. 每次使用时虽然理解了,但由于没有去 ...
- AutoResetEvent和ManualResetEvent理解
AutoResetEvent和ManualResetEvent用于多线程之间代码执行顺序的控制,它们继承自WaitHandle,API相同,但在使用中还是有区别的. 每次使用时虽然理解了,但由于没有去 ...
- C# Event 内核构造 |EventWaitHandle、AutoResetEvent、 ManualResetEvent
EventWaitHandle 继承:Object->WaitHandle-> EventWaitHandle派生:System.Threading.AutoResetEvent\Syst ...
- C# 同步转异步 AutoResetEvent
当我们的程序运行时,调用了一段异步的逻辑A,这段异步的逻辑无法转化为同步(如动画.下载进度等) 而,我们又需要等待异步逻辑A处理完成,然后再执行其它逻辑B. AutoResetEvent 同步转异步 ...
- C#与C++的发展历程第三 - C#5.0异步编程巅峰
系列文章目录 1. C#与C++的发展历程第一 - 由C#3.0起 2. C#与C++的发展历程第二 - C#4.0再接再厉 3. C#与C++的发展历程第三 - C#5.0异步编程的巅峰 C#5.0 ...
- C#并发编程
并发编程,一直是小白变成(●—●)的一个坎.平时也用到过不少并发编程操作,在这里进行一下记录. 多线程并不是唯一 并发:同时做多件事情. 多线程:并发的一种形式,采用多线程来执行程序. 并行处理:把正 ...
- C#多线程操作界面控件的解决方案(转)
C#中利用委托实现多线程跨线程操作 - 张小鱼 2010-10-22 08:38 在使用VS2005的时候,如果你从非创建这个控件的线程中访问这个控件或者操作这个控件的话就会抛出这个异常.这是微软为了 ...
随机推荐
- 在Linux中打印函数调用堆栈【原创】
本人学习笔记,代码参考如下网址 参考http://www.cnblogs.com/dma1982/archive/2012/02/08/2342215.html zhangbh@prolin-srv: ...
- java中OutputStream字节流与字符流InputStreamReader 每一种基本IO流BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWriter,FileInputStream,FileReader,FileWriter,InputStr
BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWri ...
- du -sh
评估文件空间利用率: [root@vm-xiluhua][/home]$ du -sh /home 409M /home --exclude选项,排除指定模式的文件的大小 [root@vm-xiluh ...
- 常用的math函数
<?php //1.abs — 绝对值 echo abs(-77); //ceil — 进一法取整 ...
- 删除 SQL Server 2005 Express 工具
安装sql server 2008 management,提示错误:Sql2005SsmsExpressFacet 检查是否安装了 SQL Server 2005 Express 工具. 失败,已安装 ...
- 文件名唯一(A.txt => An.txt)
/// <summary> /// 文件名唯一(A.txt => An.txt) /// </summary> /// <param name="full ...
- 在PostgreSQL中使用oracle_fdw访问Oracle
本文讲述如何在PostgreSQL中使用oracle_fdw访问Oracle上的数据. 1. 安装oracle_fdw 可以参照:oracle_fdw in github 编译安装oracle_fdw ...
- ACM题目————二叉树的遍历
一.二叉树的后序遍历: 题目描述 给定一颗二叉树,要求输出二叉树的深度以及后序遍历二叉树得到的序列.本题假设二叉树的结点数不超过1000 输入 输 入数据分为多组,第一行是测试数据的组数n,下面的n行 ...
- 【转】【Android】开源项目汇总-备用
第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...
- uva 1025,城市的间谍
题目链接:https://uva.onlinejudge.org/external/10/1025.pdf 题意: 地铁是线性的,有n个站,编号(1~n),M1辆从左至右的车,和M2辆从右至左的车,发 ...