AutoResetEvent和ManualResetEvent
本文在于巩固基础
AutoResetEvent
概念:通知正在等待的线程已发生的事件
如果AutoResetEvent 为非终止状态,则线程会被阻止,并等待当前控制资源的线程通过调用 Set 来通知资源可用。 调用Set 向AutoResetEvent 发信号以释放等待线程。
可以通过构造函数设置事件状态 false为非终止,true为终止
常用方法:
Set();将事件状态设置为终止状态,允许一个或多个等待线程继续。
ReSet();将事件状态设置为非终止状态,导致线程阻止
WaitOne();阻止当前线程,直到当前 WaitHandle 收到信号。
ManualResetEvent用法差不多,有一点差别,在下面可以看到
下面通过一个简单实例来演示这个基本用法
class Program
{
//设置事件状态为非终止 private static ManualResetEvent _manualResetEvent=new ManualResetEvent(false);
private static Thread _thread = new Thread(new ThreadStart(ThreadMethod));
private static int _count = ;
static void Main(string[] args)
{
_thread.Start();
Console.ReadLine();
_manualResetEvent.Set();// 通知等待线程继续 } private static void ThreadMethod()
{
while (true)
{
_manualResetEvent.WaitOne();//由于上面设置事件状态为非终止所以下面语句不会执行,除非收到可以继续的信号
Thread.Sleep();
Console.WriteLine(_count++); }
}
一开始不会有输出,直到你输入数据后线程才会继续
结果如下
W
0
1
2
3
4
5
6
7
8
...
注意这里AntoResetEvent中的WaitOne方法会切换信号(不怎么专业,明白就好),也就是,这次是收到信号,下次就变成无信号,再执行又有信号,如此往复
下面是AntoResetEvent实现
class Program
{
//设置事件状态为非终止 private static AutoResetEvent _autoResetEvent=new AutoResetEvent(false);
private static Thread _thread = new Thread(new ThreadStart(ThreadMethod));
private static int _count = ;
static void Main(string[] args)
{
_thread.Start();
Console.ReadLine();
_autoResetEvent.Set(); } private static void ThreadMethod()
{
while (true)
{
_autoResetEvent.WaitOne();//由于上面设置事件状态为非终止所以下面语句不会执行,除非收到可以继续的信号
Thread.Sleep();
Console.WriteLine(_count++);
_autoResetEvent.Set();//上面的WaitOne执行后变为无信号,这样下次循环就阻塞了,这步可以使下次循环接收到继续的信号 }
} }
AutoResetEvent和ManualResetEvent的更多相关文章
- C#多线程同步事件及等待句柄AutoResetEvent 和 ManualResetEvent
最近捣鼓了一下多线程的同步问题,发现其实C#关于多线程同步事件处理还是很灵活,这里主要写一下,自己测试的一些代码,涉及到了AutoResetEvent 和 ManualResetEvent,当然还有也 ...
- 多线程间通信之AutoResetEvent和ManualResetEvent的原理分析
AutoResetEvent 允许线程通过发信号互相通信. 通常,当线程需要独占访问资源时使用该类. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号. 如果 AutoRe ...
- 个人对AutoResetEvent和ManualResetEvent的理解(转载)
仅个人见解,不对之处请指正,谢谢. 一.作用 AutoResetEvent和ManualResetEvent可用于控制线程暂停或继续,拥有重要的三个方法:WaitOne.Set和Reset. 这三个方 ...
- AutoResetEvent和ManualResetEvent理解 z
AutoResetEvent和ManualResetEvent用于多线程之间代码执行顺序的控制,它们继承自WaitHandle,API相同,但在使用中还是有区别的. 每次使用时虽然理解了,但由于没有去 ...
- C#AutoResetEvent和ManualResetEvent的区别
一:终止状态和非终止状态 首先说说线程的终止状态和非终止状态.AutoResetEvent和ManualResetEvent的构造函数中,都有bool变量来指明线程的终止状态和非终止状态.true表示 ...
- 线程同步(AutoResetEvent与ManualResetEvent)
前言 在我们编写多线程程序时,会遇到这样一个问题:在一个线程处理的过程中,需要等待另一个线程处理的结果才能继续往下执行.比如:有两个线程,一个用来接收Socket数据,另一个用来处理Socket数据, ...
- AutoResetEvent与ManualResetEvent区别
本文来自:http://www.360doc.com/content/10/1126/10/3267996_72536817.shtml 在.Net多线程编程中,AutoResetEvent和Manu ...
- WaitHandle、AutoResetEvent、ManualResetEvent
多线程中的锁系统(三)-WaitHandle.AutoResetEvent.ManualResetEvent 介绍 本章主要说下基于内核模式构造的线程同步方式,事件,信号量. 目录 一:理论 二:Wa ...
- 对比AutoResetEvent和ManualResetEvent
ManualResetEvent和AutoResetEvent 比较 ManualResetEvent和AutoResetEvent都继承自EventWaitHandler,它们的唯一区别就在于父类 ...
- c# AutoResetEvent和ManualResetEvent
网上有很多AutoResetEvent和ManualResetEvent的详细介绍,在这里不做过多详细的解释,写下自己的一点心得留作备忘. AutoResetEvent和ManualResetEven ...
随机推荐
- 线性表之顺序表(C语言实现)
线性表是从数据元素的逻辑结构上定义的. 这种数据元素的逻辑结构的特征如下: 1.除开第一个和最后一个元素之外.所有元素都有一个前驱元素和后继元素. 2.第一个元素无前驱元素,但有后继元素. 3.最后一 ...
- 【LeetCode练习题】Merge Sorted Array
Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...
- 升级Xcode7后所要更改的配置
http://blog.csdn.net/huxiaoqiao163/article/details/48711077
- Pet(dfs+vector)
Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- nagios插件之监控if8接口日志(新接口)
vi check_if8_log.c #include <stdio.h> #include <stdlib.h> #include <string.h> #inc ...
- 固定表格行列(expression)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...
- 查看oracle锁及解决办法
SQL> select t2.username,t2.sid,t2.serial#,t2.logon_time from v$locked_object t1, v$session t2 whe ...
- odbc连接数据库
using System; using System.Collections.Generic; using System.Text; using Console = System.Console; u ...
- Android面试必备
好吧,说实话是自己面试被问到的和自己整理的别人的一些问题,很多问题的回答可能根据水平不同层次和深度不同,如果经常忘的话可能是还没明白原理,学习就是对对抗遗忘,现在开始复习吧: 每个面试的问题都极大的贴 ...
- [Linked List]Linked List Cycle,Linked List Cycle II
一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linke ...