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的时候,如果你从非创建这个控件的线程中访问这个控件或者操作这个控件的话就会抛出这个异常.这是微软为了 ...
随机推荐
- Ubuntu检测磁盘是否挂载
Ubuntu默认不自动挂载磁盘. 只是学习Bash使用,需优化如使用 # file: mountAll.sh # include color support # a list of variables ...
- NEON简介【转】
转自:http://blog.csdn.net/fengbingchun/article/details/38020265 版权声明:本文为博主原创文章,未经博主允许不得转载. “ARM Advanc ...
- spring.xml命名空间
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...
- jar包里查找指定的class文件,排查是否存在或重复,工具软件:Java Class Finder
jar包里查找指定的class文件,排查是否存在或重复,工具软件:Java Class Finder 1,下载工具地址:www.idesksoft.com/classfinder.html,如图: 2 ...
- error:could not open D:\java\jre1.8\lib\i386\jvm.cfg
复制一份jre到eclipse的目录下就可以了.
- 面向生产环境的大集群模式安装Hadoop
一.实验说明 1.本实验将使用DNS而不是hosts文件解析主机名: 2.使用NFS共享密钥文件,而不是逐个手工拷贝添加密钥: 3.复制Hadoop时使用批量拷贝脚本而不是逐台复制. 测试环境: Ho ...
- drupal 自定义表单调用autocomplete主标签实现方法
代码如下: <php function module_name_form() { $form = array(); $form['city'] = array( '#title' => t ...
- 19、文件上传与下载/JavaMail邮件开发
回顾: 一. 监听器 生命周期监听器 ServletRequestListener HttpSessionListener ServletContextListener 属性监听器 ServletRe ...
- SDUT 2413:n a^o7 !
n a^o7 ! Time Limit: 1000MS Memory limit: 65536K 题目描述 All brave and intelligent fighters, next you w ...
- android 入门 006(sqlite增删改查)
android 入门 006(sqlite增删改查) package cn.rfvip.feb_14_2_sqlite; import android.content.Context; import ...