AutoResetEvent 和 ManualResetEvent 多线程应用
AutoResetEvent
1.用于在多线程,对线程进行阻塞放行
static AutoResetEvent auth0 = new AutoResetEvent(false);
static AutoResetEvent auth1 = new AutoResetEvent(false);
static void Main(string[] args)
{ Thread th = new Thread(t0);
th.IsBackground = true;
th.Start(); Thread th1 = new Thread(t1);
th1.IsBackground = true;
th1.Start(); Console.ReadLine();
}
static void t0()
{
Console.WriteLine("");
auth1.Set(); auth0.WaitOne();
Console.WriteLine("");
auth1.Set(); auth0.WaitOne();
Console.WriteLine("");
auth1.Set();
}
static void t1()
{
auth1.WaitOne();
Console.WriteLine("");
auth0.Set(); auth1.WaitOne();
Console.WriteLine("");
auth0.Set(); auth1.WaitOne();
Console.WriteLine("");
}
多个线程对应多个 AutoResetEvent 实例,初始化设置阻塞false,WaitOne进行阻塞,当Set之后阻塞变成true程序进行,另外WaitOne之后AutoResetEvent会自动变成fase。
Set之后,若多个线程都WaitOne,会随机向某个线程发送继续执行的信号。
执行结果如图
ManualResetEvent
2.Set之后多个线程会收到放行信号
static ManualResetEvent mAuth0 = new ManualResetEvent(false);
static ManualResetEvent mAuth1 = new ManualResetEvent(false);
static CircleQueue<bool> MyQueue = new CircleQueue<bool>();
static void Main(string[] args)
{
Thread th = new Thread(t0);
th.IsBackground = true;
th.Start(); Thread th1 = new Thread(t1);
th1.IsBackground = true;
th1.Start(); Thread th2 = new Thread(t2);
th2.IsBackground = true;
th2.Start(); Console.ReadLine();
}
static void t0()
{
Console.WriteLine("1-0");
mAuth1.Set(); mAuth0.WaitOne();
Console.WriteLine("2-0");
mAuth1.Set();
}
static void t1()
{
bool r = mAuth1.WaitOne();
MyQueue.EnQueue(r);
Console.WriteLine("2-1");
while (true)
{
bool[] ListA = MyQueue.GetAllQueue();
if (ListA.Contains(false) == false)
{
break;
}
Thread.Sleep();
}
mAuth1.Reset();
mAuth0.Set(); mAuth1.WaitOne();
Console.WriteLine("3-1");
}
static void t2()
{
bool r = mAuth1.WaitOne();
MyQueue.EnQueue(r);
Console.WriteLine("2-2");
while (true)
{
bool[] ListA = MyQueue.GetAllQueue();
if (ListA.Contains(false) == false)
{
break;
}
Thread.Sleep();
}
mAuth1.Reset();
mAuth0.Set(); mAuth1.WaitOne();
Console.WriteLine("3-2");
}
运行结果
其中用到了环形队列,下次再讨论。
AutoResetEvent 和 ManualResetEvent 多线程应用的更多相关文章
- C#多线程同步事件及等待句柄AutoResetEvent 和 ManualResetEvent
最近捣鼓了一下多线程的同步问题,发现其实C#关于多线程同步事件处理还是很灵活,这里主要写一下,自己测试的一些代码,涉及到了AutoResetEvent 和 ManualResetEvent,当然还有也 ...
- 多线程间通信之AutoResetEvent和ManualResetEvent的原理分析和开发示例
AutoResetEvent 允许线程通过发信号互相通信. 通常,当线程需要独占访问资源时使用该类. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号. 如果 AutoRe ...
- 多线程间通信之AutoResetEvent和ManualResetEvent的原理分析
AutoResetEvent 允许线程通过发信号互相通信. 通常,当线程需要独占访问资源时使用该类. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号. 如果 AutoRe ...
- c# 多线程编程中AutoResetEvent和ManualResetEvent
作为等同于Java的wait,notify,notifyAll的存在,AutoResetEvent和ManualResetEvent分别实现了notify和notifyAll的功能,下面的代码简单讲解 ...
- AutoResetEvent和ManualResetEvent(多线程操作)
摘自风中灵药的博客:https://www.cnblogs.com/qingyun163/archive/2013/01/05/2846633.html#!comments AutoResetEven ...
- C# 应用 - 多线程 6) 处理同步数据之手动同步 AutoResetEvent 和 ManualResetEvent
1. 类的关系 AutoResetEvent 和 ManualResetEvent 都继承自 System.Threading.EventWaitHandle 类(EventWaitHandle 继承 ...
- AutoResetEvent和ManualResetEvent理解 z
AutoResetEvent和ManualResetEvent用于多线程之间代码执行顺序的控制,它们继承自WaitHandle,API相同,但在使用中还是有区别的. 每次使用时虽然理解了,但由于没有去 ...
- 线程同步(AutoResetEvent与ManualResetEvent)
前言 在我们编写多线程程序时,会遇到这样一个问题:在一个线程处理的过程中,需要等待另一个线程处理的结果才能继续往下执行.比如:有两个线程,一个用来接收Socket数据,另一个用来处理Socket数据, ...
- AutoResetEvent与ManualResetEvent区别
本文来自:http://www.360doc.com/content/10/1126/10/3267996_72536817.shtml 在.Net多线程编程中,AutoResetEvent和Manu ...
随机推荐
- centos6.9系统安装
1. 选择系统及下载 CentOS 5.x CentOS 6.x 50% 6.9 CentOS 7.x 50% 7.2 centos 6.9 centos 7. 最新版 https://wiki.ce ...
- 用描述符实现缓存功能和property实现原理
class Lazyproperty: def __init__(self, func): self.func = func def __get__(self, instance, owner): p ...
- [USACO]奶牛博览会(DP)
Description 奶牛想证明他们是聪明而风趣的.为此,贝西筹备了一个奶牛博览会,她已经对N头奶牛进行了面试,确定了每头奶牛的智商和情商. 贝西有权选择让哪些奶牛参加展览.由于负的智商或情商会造成 ...
- V4L2学习(三)框架分析
整个v4l2的框架分为三层: 在应用层,我们可以在 /dev 目录发现 video0 类似的设备节点,上层的摄像头程序打开设备节点进行数据捕获,显示视频画面.设备节点的名字很统一,video0 vid ...
- Fibonacci again and again HDU - 1848
任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的: F(1)=1; F(2)=2; F(n)=F(n-1)+F(n-2)(n>=3); 所以,1, ...
- 笔记-HTTP代理
笔记-HTTP代理 1. 基本原理 所谓代理,一个重要的目的是身份伪装,让被访问端无法正辨识访问者的身份. 那么下一个问题是服务器从哪些信息上来识别访问端的身份. 以php http服务器为 ...
- LoadRunner11的安装和使用及其注意点(测试系统是win7)
一.安装 LoadRunner11的下载地址:http://www.ddooo.com/softdown/61971.htm 链接标题里[loadrunner11 中文破解版]实质上下载下来是没有破解 ...
- document.domain跨子域
document.domain 用来得到当前网页的域名.比如在地址栏里输入: javascript:alert(document.domain); //www.315ta.com 我们也可以给docu ...
- Core Java的那点事儿之ArrayList
Core Java的那点事儿之ArrayList 万丈高楼平地起,Java基础要拿起.今天就从我看的Core Java里找了些小基础点来分享一下. 首先隆重介绍一下专业级龙套演员---Employee ...
- MOTCF 没时间解释了 条件竞争漏洞
moctf 没时间解释了 条件竞争漏洞 题目链接 条件竞争: 在本题目中,上传文件的时候服务器无条件的接收任何类型的文件,但是你上传之后服务器会给你的文件内容修改为too slow. 比如你上传了一句 ...