1. 程式人生 > >C#多線程之 ManualResetEvent和AutoResetEvent

C#多線程之 ManualResetEvent和AutoResetEvent

允許 container 參數 ber dex threads thread res esp

初次體驗

ManualResetEvent和AutoResetEvent主要負責多線程編程中的線程同步;以下一段是引述網上和MSDN的解析:

在.Net多線程編程中,AutoResetEvent和ManualResetEvent這兩個類經常用到, 他們的用法很類似,但也有區別。Set方法將信號置為發送狀態,Reset方法將信號置為不發送狀態,WaitOne等待信號的發送。可以通過構造函數的參數值來決定其初始狀態,若為true則非阻塞狀態,為false為阻塞狀態。如果某個線程調用WaitOne方法,則當信號處於發送狀態時,該線程會得到信號, 繼續向下執行。其區別就在調用後,AutoResetEvent.WaitOne()每次只允許一個線程進入,當某個線程得到信號後,AutoResetEvent會自動又將信號置為不發送狀態,則其他調用WaitOne的線程只有繼續等待.也就是說,AutoResetEvent一次只喚醒一個線程;而ManualResetEvent則可以喚醒多個線程,因為當某個線程調用了ManualResetEvent.Set()方法後,其他調用WaitOne的線程獲得信號得以繼續執行,而ManualResetEvent不會自動將信號置為不發送。也就是說,除非手工調用了ManualResetEvent.Reset()方法,則ManualResetEvent將一直保持有信號狀態,ManualResetEvent也就可以同時喚醒多個線程繼續執行。

本質上AutoResetEvent.Set()方法相當於ManualResetEvent.Set()+ManualResetEvent.Reset();

因此AutoResetEvent一次只能喚醒一個線程,其他線程還是堵塞

生動示例

用一個三國演義的典故來寫段示例代碼:

話說曹操率領80W大軍準備圍剿劉備和孫權,面對敵眾我寡的情況,諸葛亮與周瑜想到了一個妙計,用裝滿火藥桶的大船去沖擊曹操連在一起的戰船,計劃都安排好了,可謂“萬事俱備 只欠東風”。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace Test { class Program { //默認信號為不發送狀態 private static ManualResetEvent mre = new ManualResetEvent(false); static void Main(string
[] args) { EastWind wind = new EastWind(mre); //啟動東風的線程 Thread thd = new Thread(new ThreadStart(wind.WindComming)); thd.Start(); mre.WaitOne();//萬事俱備只欠東風,事情卡在這裏了,在東風來之前,諸葛亮沒有進攻 //東風到了,可以進攻了 Console.WriteLine("諸葛亮大吼:東風來了,可以進攻了,滿載燃料的大船接著東風沖向曹操的戰船"); Console.ReadLine(); } } /// <summary> /// 傳說中的東風 /// </summary> class EastWind { ManualResetEvent _mre; /// <summary> /// 構造函數 /// </summary> /// <param name="mre"></param> public EastWind(ManualResetEvent mre) { _mre = mre; } /// <summary> /// 風正在吹過來 /// </summary> public void WindComming() { Console.WriteLine("東風正在吹過來"); for (int i = 0; i <= 5; i++) { Thread.Sleep(500); Console.WriteLine("東風吹啊吹,越來越近了..."); } Console.WriteLine("東風終於到了"); //通知諸葛亮東風已到,可以進攻了,通知阻塞的線程可以繼續執行了 _mre.Set(); } } }

運行結果:

技術分享

C#多線程之 ManualResetEvent和AutoResetEvent