本文實例講述了C#簡單多線程同步和優(yōu)先權(quán)用法。分享給大家供大家參考。具體分析如下:
本文實例題目如下:
麥當(dāng)勞有兩個做漢堡的廚師(工號:11,12)和三個銷售人員(工號:21,22,23)。
廚師生產(chǎn)漢堡,并負(fù)責(zé)將做好的漢堡放入貨架,貨架臺大小有限,最多放6個漢堡,11和12不能同時往貨架臺上放漢堡,11具有優(yōu)先權(quán)。
銷售人員負(fù)責(zé)銷售食品,三個銷售人員取食品時,貨架不能為空,三人不能同時取,23優(yōu)先權(quán)最高,21最低。21賣的最快,取得頻率最高,22次之。
一天的工作量是銷售70個漢堡。
這里先來了解一些概念:
阻塞:函數(shù)返回結(jié)果之前,線程被掛起
非阻塞:函數(shù)執(zhí)行完立即返回,不會阻塞線程
同步:函數(shù)沒有執(zhí)行完不返回,線程被掛起;
異步:函數(shù)立即返回,結(jié)果通過事件或是信號通知調(diào)用者;
同步消息處理就好比linux中簡單的read/write操作,它們需要等待這操作成功才能返回;而異步處理機(jī)制就是類似于select/poll之類的多路復(fù)用IO操作,當(dāng)所關(guān)注的消息被觸發(fā)時,由消息觸發(fā)機(jī)制通知觸發(fā)對消息的處理.
進(jìn)程:當(dāng)一個程序運行時,它就是一個進(jìn)程,進(jìn)程包括運行中的程序所使用到的內(nèi)存和系統(tǒng)資源,同時一個進(jìn)程可以包括多個線程
線程:線程是程序中的一個執(zhí)行流,每個線程都有自己的專有寄存器(棧指針、程序計數(shù)器等),但代碼區(qū)是共享的,即不同的線程可以執(zhí)行同樣的函數(shù)。
多線程:多線程是指程序中包含多個執(zhí)行流,即在一個程序中可以同時運行多個不同的線程來執(zhí)行不同的任務(wù),也就是說允許單個程序創(chuàng)建多個并行執(zhí)行的線程來完成各自的任務(wù)。時間片有CPU分配運行!
Thread主要方法:Strart(),Sleep(int),Abort(),Suspend(),Resume()
線程優(yōu)先級:在C#應(yīng)用程序中,用戶可以設(shè)定5個不同的優(yōu)先級,由高到低分別是Highest,AboveNormal,Normal,BelowNormal,Lowest,在創(chuàng)建線程時如果不指定優(yōu)先級,那么系統(tǒng)默認(rèn)為ThreadPriority.Normal。
線程同步(Framework中已經(jīng)為我們提供了三個加鎖的機(jī)制,分別是Monitor類、Lock關(guān)鍵字和Mutex類。
① C#提供了一個關(guān)鍵字lock,它可以把一段代碼定義為互斥段(critical section),互斥段在一個時刻內(nèi)只允許一個線程進(jìn)入執(zhí)行,而其他線程必須等待。
在C#中,關(guān)鍵字lock定義如下:
lock(expression表達(dá)式) statement_block
② Monitor主要用法
Monitor.Enter(obj);(expression)Monitor.Exit(obj);
③ Mutex用法
Mutex mutex = new Mutex();mutex.WaitOne();(expression)mutex.ReleaseMutex();
舉個lock互斥例子(包括多線程使用和多線程優(yōu)先權(quán)、同步使用):
using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace TestThread { class Program { private static object lockObject = new object(); private static object lockObject2 = new object(); private static int iGetHBnum = 0; private static int iPutHBnum = 0; private static int count21 = 0; private static int count22 = 0; private static int count23 = 0; private static int count11 = 0; private static int count12 = 0; static void Main(string[] args) { Console.WriteLine("主線程運行,線程ID:" + Thread.CurrentThread.ManagedThreadId.ToString()); Thread chushi1 = new Thread(PutHB); chushi1.Priority = ThreadPriority.AboveNormal; chushi1.Name = "11"; chushi1.Start(); Thread chushi2 = new Thread(PutHB); chushi2.Priority = ThreadPriority.Normal; chushi2.Name = "12"; chushi2.Start(); Thread Consume21 = new Thread(GetHB); Consume21.Priority = ThreadPriority.Normal; Consume21.Name = "21"; Consume21.Start(); Thread Consume22 = new Thread(GetHB); Consume22.Priority = ThreadPriority.Normal; Consume22.Name = "22"; Consume22.Start(); Thread Consume23 = new Thread(GetHB); Consume23.Priority = ThreadPriority.Normal; Consume23.Name = "23"; Consume23.Start(); Console.ReadKey(); } public static void PutHB() { string strID = Thread.CurrentThread.Name.ToString(); Console.WriteLine("{0}廚師開始制作漢堡,,,", strID); while (true) { if (iPutHBnum >= 6) { Console.WriteLine("廚師{0},最多放6個漢堡,請讓銷售員取再放!", strID); Thread.Sleep(1000); } else { if (iGetHBnum >= 70 ||count11 + count12 >= 70) { if (strID == "11") { Console.WriteLine("廚師{0},在貨架放共放{1}漢堡!", strID, count11); } else if (strID == "12") { Console.WriteLine("廚師{0},在貨架放共放{1}漢堡!", strID, count12); } break; } lock (lockObject) { iPutHBnum++; } if (strID == "11") { count11++; Console.WriteLine("廚師{0},在貨架放已放{1}漢堡! 現(xiàn)在貨架有{2}漢堡!", strID,count11, iPutHBnum); } else if (strID == "12") { count12++; Console.WriteLine("廚師{0},在貨架放已放{1}漢堡! 現(xiàn)在貨架有{2}漢堡!", strID, count12, iPutHBnum); } } } } public static void GetHB() { string strID = Thread.CurrentThread.Name.ToString(); Console.WriteLine("{0}銷售員取漢堡,,,", strID); while (true) { if (iPutHBnum <= 0) { Thread.Sleep(1000); Console.WriteLine("{0}貨架臺已0個漢堡,請等待廚師制作!", strID); } else { lock (lockObject2) { iGetHBnum++; iPutHBnum--; } if (strID == "23") { count23++; Console.WriteLine("23號銷售員已銷售---{0}!",count23); Thread.Sleep(3000); } else if (strID == "22") { count22++; Console.WriteLine("22號銷售員已銷售---{0}!", count22); Thread.Sleep(2000); } else if (strID == "21") { count21++; Console.WriteLine("21號銷售員已銷售---{0}!", count21); Thread.Sleep(1000); } } if (iGetHBnum >= 70) { Console.WriteLine("銷售完!"); if (strID == "23") { Console.WriteLine("23號銷售員銷售總數(shù):{0}", count23); } else if (strID == "22") { Console.WriteLine("22號銷售員銷售總數(shù):{0}", count22); } else if (strID == "21") { Console.WriteLine("21號銷售員銷售總數(shù):{0}", count21); } break; } } } } } 運行結(jié)果如下圖所示:

lock可以用Monitor,Mutex替代:
Monitor.Enter(lockObject);iPutHBnum++;Monitor.Exit(lockObject);
或者:
mutex1.WaitOne(); iPutHBnum++;mutex1.ReleaseMutex();
希望本文所述對大家的C#程序設(shè)計有所幫助。
新聞熱點
疑難解答
圖片精選