詳細解析C#多線程同步事情及等候句柄。本站提示廣大學習愛好者:(詳細解析C#多線程同步事情及等候句柄)文章只能為提供參考,不一定能成為您想要的結果。以下是詳細解析C#多線程同步事情及等候句柄正文
最近搗鼓了一下多線程的同步問題,發現其實C#關於多線程同步事情處置還是很靈敏,這裡次要寫一下,自己測試的一些代碼,觸及到了AutoResetEvent 和 ManualResetEvent,當然還有也扼要提了一下System.Threading.WaitHandle.WaitOne 、System.Threading.WaitHandle.WaitAny和System.Threading.WaitHandle.WaitAll ,上面我們一最初學者的角度來看,多線程之間的同步。
假定有這樣的一個場景,主線程開了一個子線程,讓子線程等著,等主線程完成了某件事情時再告訴子線程去往下執行,這裡關鍵就在於這個怎讓子線程等著,主線程怎告訴子線程,普通狀況下我們不難想到用一個公共變量,於是我們就有了上面的代碼:
using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace AutoResetEventTest { class Class1 { static bool flag = true; static void DoWork() { Console.WriteLine(" worker thread started, now waiting on event..."); while (flag) { } Console.WriteLine(" worker thread reactivated, now exiting..."); } static void Main() { Console.WriteLine("main thread starting worker thread..."); Thread t = new Thread(DoWork); t.Start(); Console.WriteLine("main thrad sleeping for 1 second..."); Thread.Sleep(1000); Console.WriteLine("main thread signaling worker thread..."); flag = false; } } }
雖然目的到達了,但是看著這代碼就糾結,上面該是我們的配角上場了,AutoResetEvent 和 ManualResetEvent,關於這兩者我們暫且以為是差不多了,稍後我會引見他們的不同,這裡以AutoResetEvent為例,其實很多官方的說法太過於籠統,這裡淺顯地講,可以以為AutoResetEvent就是一個公共的變量(雖然它是一個事情),創立的時分可以設置為false,然後在要等候的線程運用它的WaitOne辦法,那麼線程就不斷會處於等候形態,只要這個AutoResetEvent被別的線程運用了Set辦法,也就是要發告訴的線程運用了它的Set辦法,那麼等候的線程就會往下執行了,Set就是發信號,WaitOne是等候信號,只要發了信號,等候的才會執行。假如不發的話,WaitOne前面的順序就永遠不會執行。好上面看用AutoResetEvent改造下面的順序:
using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace AutoResetEventTest { class Class2 { static AutoResetEvent mEvent=new AutoResetEvent(false); //static ManualResetEvent mEvent = new ManualResetEvent(false); static void DoWork() { Console.WriteLine(" worker thread started, now waiting on event..."); mEvent.WaitOne(); Console.WriteLine(" worker thread reactivated, now exiting..."); } static void Main() { Console.WriteLine("main thread starting worker thread..."); Thread t = new Thread(DoWork); t.Start(); Console.WriteLine("main thrad sleeping for 1 second..."); Thread.Sleep(1000); Console.WriteLine("main thread signaling worker thread..."); mEvent.Set(); } } }
這時代碼是不是清新多了,這裡其實你還會看到,把下面的AutoResetEvent換成ManualResetEvent也是沒有問題的,那麼它兩之間的區別是什麼呢?團體以為它們最大的區別在於,無論何時,只需 AutoResetEvent 激活線程,它的形態將自動從終止變為非終止。相反,ManualResetEvent 允許它的終止形態激活恣意多個線程,只要當它的 Reset 辦法被調用時才復原到非終止形態。開上面的代碼:
using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace AutoResetEventTest { class Class3 { static AutoResetEvent mEvent = new AutoResetEvent(false); //static ManualResetEvent mEvent = new ManualResetEvent(false); static void DoWork() { Console.WriteLine(" worker thread started, now waiting on event..."); for (int i = 0; i < 3; i++) { mEvent.WaitOne(); //mEvent.Reset(); Console.WriteLine(" worker thread reactivated, now exiting..."); } } static void Main() { Console.WriteLine("main thread starting worker thread..."); Thread t = new Thread(DoWork); t.Start(); for (int i = 0; i < 3; i++) { Thread.Sleep(1000); Console.WriteLine("main thread signaling worker thread..."); mEvent.Set(); } } } }
假如你想僅僅把AutoResetEvent換成ManualResetEvent的話,你發現輸入就會亂套了,為什麼呢?
假設有autoevent.WaitOne()和manualevent.WaitOne(),當線程失掉信號後都得以持續執行。差異就在調用後,autoevent.WaitOne()每次只允許一個線程進入,當某個線程失掉信號(也就是有其他線程調用了autoevent.Set()辦法後)後,autoevent會自動又將信號置為不發送形態,則其他調用WaitOne的線程只要持續等候,也就是說,autoevent一次只喚醒一個線程。而manualevent則可以喚醒多個線程,當某個線程調用了set辦法後,其他調用waitone的線程取得信號得以持續執行,而manualevent不會自動將信號置為不發送,也就是說,除非手工調用了manualevent.Reset()辦法,否則manualevent將不斷堅持有信號形態,manualevent也就可以同時喚醒多個線程持續執行。
在下面代碼中,假如將AutoResetEvent換成ManualResetEvent的話,只需要在waitone前面做下reset,就會到達異樣的效果。
之後我們再來個復雜的例子:
using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace AutoResetEventTest { class Class4 { public static AutoResetEvent mEvent = new AutoResetEvent(false); public static void trmain() { Thread tr = Thread.CurrentThread; Console.WriteLine("thread: waiting for an event"); mEvent.WaitOne(); Console.WriteLine("thread: got an event"); for (int x = 0; x < 10; x++) { Thread.Sleep(1000); Console.WriteLine(tr.Name + ": " + x); } } static void Main(string[] args) { Thread thrd1 = new Thread(new ThreadStart(trmain)); thrd1.Name = "thread1"; thrd1.Start(); for (int x = 0; x < 10; x++) { Thread.Sleep(900); Console.WriteLine("Main:" + x); if (5 == x) mEvent.Set(); } while (thrd1.IsAlive) { Thread.Sleep(1000); Console.WriteLine("Main: waiting for thread to stop"); } } } }
是不是更有覺得了?之後咱來看看另外幾個東東:
System.Threading.WaitHandle.WaitOne 使線程不斷等候,直到單個事情變為終止形態;
System.Threading.WaitHandle.WaitAny 阻止線程,直到一個或多個指示的事情變為終止形態;
System.Threading.WaitHandle.WaitAll 阻止線程,直到一切指示的事情都變為終止形態。
然後再來個例子,以WaitAll運用為例:
using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace AutoResetEventTest { class other { static void Main(string[] args) { Random randomGenerator = new Random(); AutoResetEvent[] resets=new AutoResetEvent[5]; for (int i = 0; i < 5; i++) { resets[i] = new AutoResetEvent(false); int wTime = randomGenerator.Next(10)+1; worker w = new worker(wTime, resets[i]); Thread thrd1 = new Thread(new ThreadStart(w.work)); thrd1.Start(); } WaitHandle.WaitAll(resets); Console.WriteLine("ALL worker done - main exiting."); } } public class worker { public string name; public int wTime; public AutoResetEvent mEvent; public worker(int w, AutoResetEvent m) { name = w.ToString(); wTime = w * 1000; mEvent = m; } public void work() { Console.WriteLine(name + " worker thread waiting for " + wTime + "...."); Thread.Sleep(wTime); Console.WriteLine(name + " worker thread back..."); mEvent.Set(); } } }
復雜來說就是,開了5個線程,每個線程隨機休眠若干秒,都完成後告訴主線程加入,這裡就開了一個AutoResetEvent數組,主線程就WaitHandle.WaitAll(resets) ,子線程休眠完後就Set1個AutoResetEvent,最後都Set完後,主線程就會往下執行。最後最後再來個買書付款取貨的例子,加深了解:
using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace AutoResetEventTest { class Program { const int numIterations = 10; static AutoResetEvent myResetEvent = new AutoResetEvent(false); static AutoResetEvent ChangeEvent = new AutoResetEvent(false); //static ManualResetEvent myResetEvent = new ManualResetEvent(false); //static ManualResetEvent ChangeEvent = new ManualResetEvent(false); static int number; //這是關鍵資源 static void Main() { Thread payMoneyThread = new Thread(new ThreadStart(PayMoneyProc)); payMoneyThread.Name = "付錢線程"; Thread getBookThread = new Thread(new ThreadStart(GetBookProc)); getBookThread.Name = "取書線程"; payMoneyThread.Start(); getBookThread.Start(); for (int i = 1; i <= numIterations; i++) { Console.WriteLine("買書線程:數量{0}", i); number = i; //Signal that a value has been written. myResetEvent.Set(); //ChangeEvent.Set(); Thread.Sleep(10); } payMoneyThread.Abort(); getBookThread.Abort(); } static void PayMoneyProc() { while (true) { myResetEvent.WaitOne(); //myResetEvent.Reset(); Console.WriteLine("{0}:數量{1}", Thread.CurrentThread.Name, number); ChangeEvent.Set(); } } static void GetBookProc() { while (true) { ChangeEvent.WaitOne(); //ChangeEvent.Reset(); Console.WriteLine("{0}:數量{1}", Thread.CurrentThread.Name, number); Console.WriteLine("------------------------------------------"); //Thread.Sleep(0); } } } }
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支持。