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. vue之小小动态按钮

    Vue是前台框架,可以独立完成前后端分离式web项目渐进式的javascript框架 ,今天我们来设计一个简单的动态按钮   具体效果图如下: 点击后会变成这样: 首先我们需要下载vue.js:htt ...

  2. Codeforces Round #435 (Div. 2) B (二分图) C(构造)

    B. Mahmoud and Ehab and the bipartiteness time limit per test 2 seconds memory limit per test 256 me ...

  3. 使用FileSystem类进行文件读写及查看文件信息

    使用FileSystem类进行文件读写及查看文件信息   在这一节我们要深入了解Hadoop的FileSystem类——这是与与hadoop的文件系统交互的重要接口.虽然我们只是着重于HDFS的实现, ...

  4. Spring核心技术(十四)——ApplicationContext的额外功能

    在前文的介绍中我们知道,org.springframework.beans.factory包提供了一些基本的功能来管理和控制Bean,甚至通过编程的方式来实现.org.springframework. ...

  5. hashlib加密模块

    python hashlib密码加密   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/dss_dssssd/article/details/828 ...

  6. eclipse 插件,直接打开文件路径

    https://github.com/samsonw/OpenExplorer/downloads 22k的小插件,意义却重大.下载之后,放到plugins里面.

  7. 五分钟搞定Linux容器

    [TechTarget中国原创] Linux容器针对特定工作负载提供了全新的灵活性与可能性.存在很多解决方案,但是没有一个解决方案能够像systemd容器那样进行快速部署.给我五分钟,本文将介绍如何使 ...

  8. Linux服务器管理员必备Linux命令TOP5

    Linux桌面环境的界面友好度.图形性能及附件工具已经大幅进化,然而Linux服务器却还没有能达到这一步. 作为系统管理员必须熟练掌握Linux命令.Linux命令的内容很多,其中的一些TOP命令对于 ...

  9. IOS开发学习笔记029-反选、全选、删除按钮的实现

    还是在上一个程序的基础上进行修改 1.反选按钮 2.全选按钮 3.删除按钮 4.其他代码优化 1.反选按钮 反选的过程就是将_deleteShops数组中得数据清空,然后将Shops中数组添加到_de ...

  10. 使用 CommandScene 类在 XNA 中创建命令场景(十二)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...