C#簡單多線程同步和優(yōu)先權(quán)用法實(shí)例
本文實(shí)例講述了C#簡單多線程同步和優(yōu)先權(quán)用法。分享給大家供大家參考。具體分析如下:
本文實(shí)例題目如下:
麥當(dāng)勞有兩個(gè)做漢堡的廚師(工號:11,12)和三個(gè)銷售人員(工號:21,22,23)。
廚師生產(chǎn)漢堡,并負(fù)責(zé)將做好的漢堡放入貨架,貨架臺大小有限,最多放6個(gè)漢堡,11和12不能同時(shí)往貨架臺上放漢堡,11具有優(yōu)先權(quán)。
銷售人員負(fù)責(zé)銷售食品,三個(gè)銷售人員取食品時(shí),貨架不能為空,三人不能同時(shí)取,23優(yōu)先權(quán)最高,21最低。21賣的最快,取得頻率最高,22次之。
一天的工作量是銷售70個(gè)漢堡。
這里先來了解一些概念:
阻塞:函數(shù)返回結(jié)果之前,線程被掛起
非阻塞:函數(shù)執(zhí)行完立即返回,不會阻塞線程
同步:函數(shù)沒有執(zhí)行完不返回,線程被掛起;
異步:函數(shù)立即返回,結(jié)果通過事件或是信號通知調(diào)用者;
同步消息處理就好比linux中簡單的read/write操作,它們需要等待這操作成功才能返回;而異步處理機(jī)制就是類似于select/poll之類的多路復(fù)用IO操作,當(dāng)所關(guān)注的消息被觸發(fā)時(shí),由消息觸發(fā)機(jī)制通知觸發(fā)對消息的處理.
進(jìn)程:當(dāng)一個(gè)程序運(yùn)行時(shí),它就是一個(gè)進(jìn)程,進(jìn)程包括運(yùn)行中的程序所使用到的內(nèi)存和系統(tǒng)資源,同時(shí)一個(gè)進(jìn)程可以包括多個(gè)線程
線程:線程是程序中的一個(gè)執(zhí)行流,每個(gè)線程都有自己的專有寄存器(棧指針、程序計(jì)數(shù)器等),但代碼區(qū)是共享的,即不同的線程可以執(zhí)行同樣的函數(shù)。
多線程:多線程是指程序中包含多個(gè)執(zhí)行流,即在一個(gè)程序中可以同時(shí)運(yùn)行多個(gè)不同的線程來執(zhí)行不同的任務(wù),也就是說允許單個(gè)程序創(chuàng)建多個(gè)并行執(zhí)行的線程來完成各自的任務(wù)。時(shí)間片有CPU分配運(yùn)行!
Thread主要方法:Strart(),Sleep(int),Abort(),Suspend(),Resume()
線程優(yōu)先級:在C#應(yīng)用程序中,用戶可以設(shè)定5個(gè)不同的優(yōu)先級,由高到低分別是Highest,AboveNormal,Normal,BelowNormal,Lowest,在創(chuàng)建線程時(shí)如果不指定優(yōu)先級,那么系統(tǒng)默認(rèn)為ThreadPriority.Normal。
線程同步(Framework中已經(jīng)為我們提供了三個(gè)加鎖的機(jī)制,分別是Monitor類、Lock關(guān)鍵字和Mutex類。
① C#提供了一個(gè)關(guān)鍵字lock,它可以把一段代碼定義為互斥段(critical section),互斥段在一個(gè)時(shí)刻內(nèi)只允許一個(gè)線程進(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();
舉個(gè)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("主線程運(yùn)行,線程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個(gè)漢堡,請讓銷售員取再放!", 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個(gè)漢堡,請等待廚師制作!", 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; } } } } }
運(yùn)行結(jié)果如下圖所示:
lock可以用Monitor,Mutex替代:
Monitor.Enter(lockObject); iPutHBnum++; Monitor.Exit(lockObject);
或者:
mutex1.WaitOne(); iPutHBnum++; mutex1.ReleaseMutex();
希望本文所述對大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#開發(fā)WinForm清空DataGridView控件綁定的數(shù)據(jù)
本文詳細(xì)講解了C#開發(fā)WinForm清空DataGridView控件綁定數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03wpf將表中數(shù)據(jù)顯示到datagrid示例
這篇文章主要介紹了wpf將表中數(shù)據(jù)顯示到datagrid示例,需要的朋友可以參考下2014-02-02解析C#中用Process類終止進(jìn)程,執(zhí)行命令的深入分析
本篇文章是對C#中用Process類終止進(jìn)程,執(zhí)行命令進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05c# 應(yīng)用NPOI獲取Excel中的圖片,保存至本地的算法
本文主要介紹了c# 應(yīng)用NPOI獲取Excel中的圖片,保存至本地的算法。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02Win10 系統(tǒng)下VisualStudio2019 配置點(diǎn)云庫 PCL1.11.0的圖文教程
這篇文章主要介紹了Win10 系統(tǒng)下VisualStudio2019 配置點(diǎn)云庫 PCL1.11.0的圖文教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07

c#使用file.copy實(shí)現(xiàn)文件備份示例

深入了解C#設(shè)計(jì)模式之訂閱發(fā)布模式