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 多线程应用的更多相关文章

  1. C#多线程同步事件及等待句柄AutoResetEvent 和 ManualResetEvent

    最近捣鼓了一下多线程的同步问题,发现其实C#关于多线程同步事件处理还是很灵活,这里主要写一下,自己测试的一些代码,涉及到了AutoResetEvent 和 ManualResetEvent,当然还有也 ...

  2. 多线程间通信之AutoResetEvent和ManualResetEvent的原理分析和开发示例

    AutoResetEvent 允许线程通过发信号互相通信. 通常,当线程需要独占访问资源时使用该类. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号. 如果 AutoRe ...

  3. 多线程间通信之AutoResetEvent和ManualResetEvent的原理分析

    AutoResetEvent 允许线程通过发信号互相通信. 通常,当线程需要独占访问资源时使用该类. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号. 如果 AutoRe ...

  4. c# 多线程编程中AutoResetEvent和ManualResetEvent

    作为等同于Java的wait,notify,notifyAll的存在,AutoResetEvent和ManualResetEvent分别实现了notify和notifyAll的功能,下面的代码简单讲解 ...

  5. AutoResetEvent和ManualResetEvent(多线程操作)

    摘自风中灵药的博客:https://www.cnblogs.com/qingyun163/archive/2013/01/05/2846633.html#!comments AutoResetEven ...

  6. C# 应用 - 多线程 6) 处理同步数据之手动同步 AutoResetEvent 和 ManualResetEvent

    1. 类的关系 AutoResetEvent 和 ManualResetEvent 都继承自 System.Threading.EventWaitHandle 类(EventWaitHandle 继承 ...

  7. AutoResetEvent和ManualResetEvent理解 z

    AutoResetEvent和ManualResetEvent用于多线程之间代码执行顺序的控制,它们继承自WaitHandle,API相同,但在使用中还是有区别的. 每次使用时虽然理解了,但由于没有去 ...

  8. 线程同步(AutoResetEvent与ManualResetEvent)

    前言 在我们编写多线程程序时,会遇到这样一个问题:在一个线程处理的过程中,需要等待另一个线程处理的结果才能继续往下执行.比如:有两个线程,一个用来接收Socket数据,另一个用来处理Socket数据, ...

  9. AutoResetEvent与ManualResetEvent区别

    本文来自:http://www.360doc.com/content/10/1126/10/3267996_72536817.shtml 在.Net多线程编程中,AutoResetEvent和Manu ...

随机推荐

  1. pycharm配置Git托管

    利用Pycharm和github管理代码转载https://www.cnblogs.com/feixuelove1009/p/5955332.html git教程--廖雪峰git教程  转载https ...

  2. django开发傻瓜教程-1-安装和HelloWorld

    安装 sudo pip install Django 新建项目 django-admin startproject XXX 启动项目 进入主目录下 python manage.py runserver ...

  3. linux-最最最最最常用的操作

    去除两个文件中相同的内容 比如我想把file1中不含文件file2的内容保留下来:(这个在抠一些内容的时候挺好用的) awk '{print $0}' file1 file2 |sort|uniq - ...

  4. poj 1258 建光迁问题 最小生成树

    题意:给全村建光纤,求花费最小 思路:最小生成树,树相对于图来说就是没有环 m用来存图 v判断是否访问 low用来存两点间的最短距离 给low赋值  for(i=1;i<=n;i++){if(i ...

  5. [原]sencha touch之布局

    今天记录一下关于sencha touch中的几种布局,其实很简单的,还是直接上代码,一目了然 1:box布局,其实就是vbox和hbox,说白了一个是横着摆放,一个是竖着摆放 Ext.applicat ...

  6. Asp.net自定义控件开发任我行(3)-Render

    摘要 上一篇我们讲到了自定义标签TagPrefix用法,此篇我们来讲一下控件的呈现,主要是呈现下拉框 内容 呈现的方法有,Render,RenderControl,RenderChildren,这三个 ...

  7. 我给女朋友讲编程CSS系列(3) CSS如何设置字体的类型、大小、颜色,如何使用火狐浏览器的Firebug插件查看网页的字体

    一.CSS如何设置字体的类型.大小.颜色 设计网页时,一般设置body的字体,让其他标签继承body的字体,这样设置特别方便,但是标题标签h1到h6和表单标签(input类型)是没有继承body的字体 ...

  8. 【Best Time to Buy and Sell Stock II】cpp

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  9. thinkphp3.2.3多图上传并且生成多张缩略图

    html部分 <!DOCTYPE html><html><head><meta http-equiv="Content-Type" con ...

  10. csu-2018年11月月赛Round2-div1题解

    csu-2018年11月月赛Round2-div1题解 A(2191):Wells的积木游戏 Description Wells有一堆N个积木,标号1~N,每个标号只出现一次 由于Wells是手残党, ...