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. 算法_NP_证明

    8.3 STINGY SAT is the following problem: given a set of clauses (each a disjunction of literals) and ...

  2. Codeforces Round #461 (Div. 2) A. Cloning Toys

    A. Cloning Toys time limit per test 1 second memory limit per test 256 megabytes Problem Description ...

  3. 动态规划:HDU-1398-Square Coins(母函数模板)

    解题心得: 1.其实此题有两种做法,动态规划,母函数.个人更喜欢使用动态规划来做,也可以直接套母函数的模板 Square Coins Time Limit: 2000/1000 MS (Java/Ot ...

  4. 按位与&、或|、异或^等运算方法

    (转载) 按位与运算符(&) 参加运算的两个数据,按二进制位进行“与”运算. 运算规则:0&0=0;   0&1=0;    1&0=0;     1&1=1; ...

  5. Codeforces 653G Move by Prime 组合数学

    题意: 有一个长度为\(n\)的正整数序列\(a\),有这样一种操作: 每次可以选序列中的某一个数乘上或除以某一个素数. 求对于每一个子序列使其所有元素相等的最少操作次数之和. 分析: 因为两个素数之 ...

  6. document.domain跨子域

    document.domain 用来得到当前网页的域名.比如在地址栏里输入: javascript:alert(document.domain); //www.315ta.com 我们也可以给docu ...

  7. 我给女朋讲编程网络系列(2)--IIS8 如何在本地发布网站

    通过IIS8 在本地发布网站,一个截图,你就全明白了,越是简单,越是实用. 如果有现成的网站,就将你的网站放到一个文件夹中,比如WebTest2中. 如何没有网站,可以在WebTest2中新建一个in ...

  8. 5、CSS基础part-3

    1.CSS列表 ①类型 ul.disc {list-style-type: disc} ②位置 ul.inside {list-style-position: inside} ③列表图像 2.表格

  9. Log4j官方文档翻译(八、文件输出)

    使用org.apache.log4j.FileAppender可以把日志写到文件中: FileAppender配置 immediateFlush 这个标志默认为true,是否每次有消息产生都自动flu ...

  10. 【bzoj2789】[Poi2012]Letters 树状数组求逆序对

    题目描述 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B. 输入 第一行一个正整数n ...