AutoResetEvent和ManualResetEvent

简介:

 AutoResetEvent 就像一个十字转门,每次只允许一个取消阻塞。

static AutoResetEvent auto = new AutoResetEvent(false);

        static void Main(string[] args)
        {
            Thread t = new Thread(WaitFoSingalToWrite);
            t.Start();
            Thread.Sleep(2000);
            auto.Set();
            Console.WriteLine("Main End...");
        }
 
        static void WaitFoSingalToWrite()
        {
            Console.WriteLine("in...");
            auto.WaitOne();
            Console.WriteLine("do sth...");
            auto.WaitOne(3000);
            Console.WriteLine("do sth 1...");
            Console.WriteLine("out...");
        }
 
初始的时候,AutoResetEvent构造函数赋值false,意味着一开始就没有信号。线程t运行后遇到block。与此同时的2秒后,Main函数对auto设置了一个信号(set)。此时t可以运行下去。接下来t等待3s后继续执行直至退出。
修改代码至如下
 static AutoResetEvent auto = new AutoResetEvent( true);
        static void Main(string[] args)
        {
            Thread t = new Thread(WaitFoSingalToWrite);
            t.Start();
            Thread.Sleep(2000);
             //auto.Set();
            Console.WriteLine("Main End...");
        }
 
        static void WaitFoSingalToWrite()
        {
            Console.WriteLine("in...");
            auto.WaitOne();
            Console.WriteLine("do sth...");
            auto.WaitOne(3000);
            Console.WriteLine("do sth 1...");
            Console.WriteLine("out...");
        }
此时尽管t线程有waitone,但是由于初始就给予了一个信号,隐藏,这个block直接就运行下去了。 If Set is called when no thread is waiting, the handle stays open for as long as it takes until some thread calls WaitOne.
此处即便对多次调用set方法,但是它还是仅对下一个waitone有效,并不是调几次set方法就对几个waitone有效,多调用的set方法纯属浪费。
 static AutoResetEvent auto = new AutoResetEvent(false);
        static void Main(string[] args)
        {
            Thread t = new Thread(WaitFoSingalToWrite);
            t.Start();
            Thread.Sleep(2000);
             auto.Set();
            auto.Set();
            Console.WriteLine("Main End...");
        }
 
        static void WaitFoSingalToWrite()
        {
            Console.WriteLine("in...");
            auto.WaitOne();
            Console.WriteLine("do sth...");
            auto.WaitOne();
            Console.WriteLine("do sth1...");
           
            Console.WriteLine("out...");
        }
     此处,虽然调用了2遍,但do sth1仍然被阻塞。
 
Reset()方法将AutoResetEvent设为无信号状态,但是此方法在AutoResetEvent并没有意义,因为AutoResetEvent发完信号让线程取消阻塞后又自动设为无信号状态了。 调用waitOne(0)相当于reset了AutoResetEvent(只要AutoResetEvent是有信号状态)

 ManualResetEvent则像一个普通的门,只要有信号,所有的阻塞都能取消,直到重新Reset。

 

 static ManualResetEvent manu = new ManualResetEvent(false);
        static void Main(string[] args)
        {
            Thread t1 = new Thread(WaitFoSingalToDo);
            Thread t2 = new Thread(WaitFoSingalToPlay);
            t1.Start();
            t2.Start();
            manu.Set(); 
            Thread.Sleep(5000);
            manu.Reset(); 
            Console.WriteLine("Main End...");
        }
 
        static void WaitFoSingalToDo()
        {
            Console.WriteLine("WaitFoSingalToDo in ...");
            manu.WaitOne();
            Console.WriteLine("do sth...");
            manu.WaitOne();
            Console.WriteLine("do sth1...");
           Console.WriteLine("out...");
        }
 
 
        static void WaitFoSingalToPlay()
        {
            Console.WriteLine("WaitFoSingalToPlay in...");
            manu.WaitOne();
            Console.WriteLine("play sth...");
            
            Thread.Sleep(10000);
            manu.WaitOne();
            Console.WriteLine("play sth1...");
 
            Console.WriteLine("out...");
        }
此处,线程t1由于已经有信号,顺畅的运行完毕。t2则运行到play sth...时,等待10秒钟,而此时,5秒钟后,manu将信号Reset(),信号被取消,,所以10秒钟过后,阻塞不能通过。




















本文转自cnn23711151CTO博客,原文链接:http://blog.51cto.com/cnn237111/518814 ,如需转载请自行联系原作者
相关文章
|
5月前
|
C#
C# | 使用AutoResetEvent和ManualResetEvent进行线程同步和通信
在多线程编程中,AutoResetEvent 和 ManualResetEvent 是两个常用的同步原语。它们用于线程间的通信和协调,以确保线程按照特定的顺序执行。本篇博客将介绍这两种同步原语的概念、用法和区别。
48 0
C# | 使用AutoResetEvent和ManualResetEvent进行线程同步和通信
|
7月前
|
Java 开发者
停止线程 & 守护线程 & 线程阻塞
停止线程 & 守护线程 & 线程阻塞
9 0
停止线程 & 守护线程 & 线程阻塞
|
Java
线程中断方法interrupt、isInterrupted、interrupted方法
线程中断方法interrupt、isInterrupted、interrupted方法
87 0
线程中断方法interrupt、isInterrupted、interrupted方法
C#深入理解AutoResetEvent和ManualResetEvent
当在C#使用多线程时就免不了使用AutoResetEvent和ManualResetEvent类,可以理解这两个类可以通过设置信号来让线程停下来或让线程重新启动,其实与操作系统里的信号量很相似(汗,考完考试已经有点忘记了)。
1871 0
C#AutoResetEvent和ManualResetEvent的区别
一:终止状态和非终止状态 首先说说线程的终止状态和非终止状态。AutoResetEvent和ManualResetEvent的构造函数中,都有bool变量来指明线程的终止状态和非终止状态。
791 0