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. 科学计算库Numpy——数组形状

    改变数组维数 给数组的shape属性赋值,改变数组的维数.数组的大小是不能改变的. 增加维度 使用np.newaxis增加维度. 删除维度 使用squeeze()删除维度是1的维度,也就是删除shap ...

  2. selenium中webdriver跳转新页面后定位置新页面的两种方式

    刚刚在写Python爬虫的时候用到了selenium , 在跳转新页面时发现无法定位新页面 , 查找不到新页面的元素 一番查询后得到了解决方法 , 便记录下来备忘 , 也与大家分享 # 页面跳转代码. ...

  3. du 与df 统计系统磁盘不一致原因与解决方法

    事件起因: 同事发现云主机磁盘系统盘满了,准备清理系统盘,便利用du 命令统计了根目录下各文件夹的大小,发现统计的各文件夹的大小总和 加起来比 df 命令查看到的系统盘所使用空间 要小很多.这里记录下 ...

  4. 2017 ACM-ICPC网络赛 H.Skiing 有向图最长路

    H.Skiing In this winter holiday, Bob has a plan for skiing at the mountain resort. This ski resort h ...

  5. A1042 Shuffling Machine (20)

    1042 Shuffling Machine (20)(20 分) Shuffling is a procedure used to randomize a deck of playing cards ...

  6. DFS:Prime Ring Problem(素数环)

    解体心得: 1.一个回溯法,可以参考八皇后问题. 2.题目要求按照字典序输出,其实在按照回溯法得到的答案是很正常的字典序.不用去特意排序. 3.输出有个坑,就是在输出一串的最后不能有空格,不然要PE, ...

  7. POJ3436------ACM Computer Factory

    题目链接 ACM Computer Factory Description As you know, all the computers used for ACM contests must be i ...

  8. spoj 104 Highways(Matrix-tree定理)

    spoj 104 Highways 生成树计数,matrix-tree定理的应用. Matrix-tree定理: D为无向图G的度数矩阵(D[i][i]是i的度数,其他的为0),A为G的邻接矩阵(若u ...

  9. MySQL之索引(四)

    压缩索引 MyISAM使用前缀压缩来减少索引的大小,从而让更多的索引可以放入内存中,这在某些情况下能极大地提高性能.默认只压缩字符串,但通过参数设置也可以对整数做压缩. MyISAM压缩每个索引块的方 ...

  10. TCP/IP网络编程之多线程服务端的实现(二)

    线程存在的问题和临界区 上一章TCP/IP网络编程之多线程服务端的实现(一)的thread4.c中,我们发现多线程对同一变量进行加减,最后的结果居然不是我们预料之内的.其实,如果多执行几次程序,会发现 ...