[.net 多线程]AutoResetEvent, ManualResetEvent
ManualResetEvent:
通知一个或多个正在等待的线程已发生事件,允许线程通过发信号互相通信,来控制线程是否可心访问资源。
- Set() : 用于向 ManualResetEvent 发送信号,使其取消阻塞状态(唤醒进程)或者开始阻塞进程,这基于 ManualResetEvent 的初始状态。
 - ReSet() : 将 ManualResetEvent 的状态重置至初始状态(即使用 Set() 方法之前的状态)。
 - WaitOne() : 使 ManualResetEvent 进入阻塞状态,开始等待唤醒信号。如果有信号,则不会阻塞,直接通过。
 - 信号 : new ManualResetEvent(bool arg) ,arg参数就是信号状态,假如为false,则表示当前无信号阻塞线程;如果为true,则有信号不阻塞。
 
static ManualResetEvent _munual = new ManualResetEvent(false);
static void Main(string[] args)
{
_munual.Reset();
for (int i = ; i <= ; i++)
{
Thread th = new Thread(() =>
{
while (true)
{
_munual.WaitOne();
Print();
Thread.Sleep();
}
});
th.IsBackground = true;
th.Name = "线程" + i;
th.Start();
} while (true)
{
Thread.Sleep();
Console.WriteLine($"主线程设置信号量");
_munual.Set();
Thread.Sleep();
_munual.Reset();
}
} static void Print()
{
Console.WriteLine($"{Thread.CurrentThread.Name}::{DateTime.Now.ToString("hh:mm:ss-fff")}");
}
ManualResetEvent示例
AutoResetEvent:
与ManualResetEvent功能类似。
static void Main(string[] args)
{
int current = ;
List <AutoResetEvent> events = new List<AutoResetEvent>();
for (int taskid = ; taskid < ; taskid++)
{
Task.Factory.StartNew(() =>
{
int curr = Interlocked.Increment(ref current);
AutoResetEvent evt = new AutoResetEvent(false);
events.Add(evt);
Console.WriteLine($"Task[{curr}]: working");
Thread.Sleep(current * );
Console.WriteLine($"Task[{curr}]: exit");
evt.Set();
});
}
Console.WriteLine($"Main wait");
AutoResetEvent.WaitAll(events.ToArray());
Console.WriteLine($"All works are done,Main exit");
Console.ReadKey();
}
AutoResetEvent示例
两只比较:
AutoResetEvent在一个线程Wait通过后,会自动设置为有信号状态重新阻塞线程。而ManualResetEvent要调用Reset方法重置,否则将一直允许后续其他线程通过。
https://blog.csdn.net/ma_jiang/article/details/78628988
https://www.cnblogs.com/zhangweizhong/p/6628442.html
[.net 多线程]AutoResetEvent, ManualResetEvent的更多相关文章
- 多线程AutoResetEvent
		
我们在线程编程的时候往往会涉及到线程的通信,通过信号的接受来进行线程是否阻塞的操作. AutoResetEvent 允许线程通过发信号互相通信.通常,此通信涉及线程需要独占访问的资源. AutoRes ...
 - C#多线程之 ManualResetEvent和AutoResetEvent
		
初次体验 ManualResetEvent和AutoResetEvent主要负责多线程编程中的线程同步:以下一段是引述网上和MSDN的解析: 在.Net多线程编程中,AutoResetEvent和Ma ...
 - 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.写的牛逼. 还有一些公司用到的风中灵药 ...
 - 多线程中的锁系统(三)-WaitHandle、AutoResetEvent、ManualResetEvent
		
本章主要介绍下基于内核模式构造的线程同步方式,事件,信号量. 阅读目录: 理论 WaitHandle AutoResetEvent ManualResetEvent 总结 理论 Windows的线程同 ...
 - 线程同步(AutoResetEvent与ManualResetEvent)
		
前言 在我们编写多线程程序时,会遇到这样一个问题:在一个线程处理的过程中,需要等待另一个线程处理的结果才能继续往下执行.比如:有两个线程,一个用来接收Socket数据,另一个用来处理Socket数据, ...
 - 【深度好文】多线程之WaitHandle-->派生EventWaitHandle事件构造-》AutoResetEvent、ManualResetEvent
		
AutoResetEvent/ManualResetEvent 都是继承自 EventWaitHandle ,EventWaitHandle继承自WaitHandle. 在讨论这个问题之前,我们先了解 ...
 - [.net 面向对象程序设计进阶] (17) 多线程(Multithreading)(二) 利用多线程提高程序性能(中)
		
[.net 面向对象程序设计进阶] (17) 多线程(Multithreading)(二) 利用多线程提高程序性能(中) 本节要点: 上节介绍了多线程的基本使用方法和基本应用示例,本节深入介绍.NET ...
 - 细说C#多线程那些事 - 线程同步和多线程优先级
		
上个文章分享了一些多线程的一些基础的知识,今天我们继续学习. 一.Task类 上次我们说了线程池,线程池的QueueUserWorkItem()方法发起一次异步的线程执行很简单 但是该方法最大的问题是 ...
 
随机推荐
- 第15届浙江省赛 E LIS
			
LIS Time Limit: 1 Second Memory Limit: 65536 KB Special Judge DreamGrid is learning the LI ...
 - 杂项-公司:联邦快递百科-un
			
ylbtech-杂项-公司:联邦快递百科 联邦快递( FedEx)是一家国际性速递集团,提供隔夜快递.地面快递.重型货物运送.文件复印及物流服务,总部设于美国田纳西州,隶属于美国联邦快递集团(FedE ...
 - 1130 Infix Expression
			
题意:给出一个语法树(二叉树),输出相应的中缀表达式. 思路:很显然,通过中序遍历来做.通过观察,发现除了根结点之外的所有非叶结点的两侧都要输出括号,故在中序遍历时判断一下即可. 代码: #inclu ...
 - Linux学习笔记 -- 初识 Shell
			
Shell 是什么 Shell 是一个用C语言编写的程序,它是用户使用Linux的桥梁.Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务.Shell既是 ...
 - java成神之——MySQL Connector/J 的基本使用
			
使用示例 DBCP连接池 结语 使用示例 public class demo { static Connection con = null; static Statement st = null; s ...
 - Python Twisted系列教程2:异步编程初探与reactor模式
			
作者:dave@http://krondo.com/slow-poetry-and-the-apocalypse/ 译者:杨晓伟(采用意译) 这个系列是从这里开始的,欢迎你再次来到这里来.现在我们可 ...
 - EF中新建表和关联表的方法
			
以机场表为例 private static AIRPORT_HELIPORTManager AirportHeliportManager => ManagerFactory.Instance.A ...
 - Eclipse maven工程生命周期clean、compile、test、package
			
1.清理. 编译 工程右键->Run As->Maven build... 清理.编译操作控制台信息输出: 2.测试 同清理.编译,goals改成test 3.打包 同清理.编译,go ...
 - leetcode893
			
class Solution { public: int numSpecialEquivGroups(vector<string>& A) { set<string> ...
 - 你不知道的js异步、作用域、闭包
			
例题如下: for (var i = 0; i < 3; i++) { setTimeout(function() { console.log(i); }, 0) ...