博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#多线程之二:ManualResetEvent和AutoResetEvent
阅读量:4618 次
发布时间:2019-06-09

本文共 2119 字,大约阅读时间需要 7 分钟。

初次体验

ManualResetEvent和AutoResetEvent主要负责多线程编程中的线程同步;以下一段是引述网上和MSDN的解析:

在.Net多线程编程中,AutoResetEvent和ManualResetEvent这两个类经常用到, 他们的用法很类似,但也有区别。Set方法将信号置为发送状态,Reset方法将信号置为不发送状态,WaitOne等待信号的发送。可以通过构造函数的参数值来决定其初始状态,若为true则非阻塞状态,为false为阻塞状态。如果某个线程调用WaitOne方法,则当信号处于发送状态时,该线程会得到信号, 继续向下执行。其区别就在调用后,AutoResetEvent.WaitOne()每次只允许一个线程进入,当某个线程得到信号后,AutoResetEvent会自动又将信号置为不发送状态,则其他调用WaitOne的线程只有继续等待.也就是说,AutoResetEvent一次只唤醒一个线程;而ManualResetEvent则可以唤醒多个线程,因为当某个线程调用了ManualResetEvent.Set()方法后,其他调用WaitOne的线程获得信号得以继续执行,而ManualResetEvent不会自动将信号置为不发送。也就是说,除非手工调用了ManualResetEvent.Reset()方法,则ManualResetEvent将一直保持有信号状态,ManualResetEvent也就可以同时唤醒多个线程继续执行。

本质上AutoResetEvent.Set()方法相当于ManualResetEvent.Set()+ManualResetEvent.Reset();

因此AutoResetEvent一次只能唤醒一个线程,其他线程还是堵塞

生动示例

用一个三国演义的典故来写段示例代码:

话说曹操率领80W大军准备围剿刘备和孙权,面对敌众我寡的情况,诸葛亮与周瑜想到了一个妙计,用装满火药桶的大船去冲击曹操连在一起的战船,计划都安排好了,可谓“万事俱备 只欠东风”。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading;
 
namespace
Test
{
    
class
Program
    
{
        
//默认信号为不发送状态
        
private
static
ManualResetEvent mre =
new
ManualResetEvent(
false
);
 
        
static
void
Main(
string
[] args)
        
{
            
EastWind wind =
new
EastWind(mre);
            
//启动东风的线程
            
Thread thd =
new
Thread(
new
ThreadStart(wind.WindComming));
            
thd.Start();
 
            
mre.WaitOne();
//万事俱备只欠东风,事情卡在这里了,在东风来之前,诸葛亮没有进攻
 
            
//东风到了,可以进攻了
            
Console.WriteLine(
"诸葛亮大吼:东风来了,可以进攻了,满载燃料的大船接着东风冲向曹操的战船"
);
            
Console.ReadLine();
        
}
    
}
 
    
/// <summary>
    
/// 传说中的东风
    
/// </summary>
    
class
EastWind
    
{
        
ManualResetEvent _mre;
 
        
/// <summary>
        
/// 构造函数
        
/// </summary>
        
/// <param name="mre"></param>
        
public
EastWind(ManualResetEvent mre)
        
{
            
_mre = mre;
        
}
 
        
/// <summary>
        
/// 风正在吹过来
        
/// </summary>
        
public
void
WindComming()
        
{
            
Console.WriteLine(
"东风正在吹过来"
);
            
for
(
int
i = 0; i <= 5; i++)
            
{
                
Thread.Sleep(500);
                
Console.WriteLine(
"东风吹啊吹,越来越近了..."
);
            
}
            
Console.WriteLine(
"东风终于到了"
);
 
            
//通知诸葛亮东风已到,可以进攻了,通知阻塞的线程可以继续执行了
            
_mre.Set();
        
}
    
}
 
}

运行结果:

转载于:https://www.cnblogs.com/kevinGao/archive/2011/12/28/2305559.html

你可能感兴趣的文章
首页调取二级、三级栏目
查看>>
IOS数据持久化的四种方式
查看>>
解决java compiler level does not match the version of the installed java project facet
查看>>
使用NPOI将多张图片导入execl
查看>>
spring IOC容器实例化Bean的方式与RequestContextListener应用
查看>>
银行业务模拟
查看>>
Monkey测试
查看>>
[NOIP 2013普及组 No.1] 计数问题
查看>>
scrapy爬虫资料汇总篇
查看>>
jQuery学习大总结(二)jQuery选择器完整介绍
查看>>
《浪潮之巅》读后感
查看>>
日期格式化函数
查看>>
Devexpress VCL Build v2015 vol 15.2 开始测试
查看>>
maven中使用tomcat进行热部署
查看>>
Bootstrap学习第二天轮播插件
查看>>
LeetCode -- Increasing Triplet Subsequence
查看>>
hdu 2114 Calculate S(n) 数论(简单题)
查看>>
hdu 5984
查看>>
解决:Incorrect line ending: found carriage return (\r) without corresponding newline (\n)
查看>>
深入学习微框架:Spring Boot
查看>>