C#多線程之線程池ThreadPool用法
一、ThreadPool
ThreadPool是.Net Framework 2.0版本中出現(xiàn)的。
ThreadPool出現(xiàn)的背景:Thread功能繁多,而且對(duì)線程數(shù)量沒(méi)有管控,對(duì)于線程的開(kāi)辟和銷(xiāo)毀要消耗大量的資源。每次new一個(gè)THread都要重新開(kāi)辟內(nèi)存。
如果某個(gè)線程的創(chuàng)建和銷(xiāo)毀的代價(jià)比較高,同時(shí)這個(gè)對(duì)象還可以反復(fù)使用的,就需要一個(gè)池子(容器),保存多個(gè)這樣的對(duì)象,需要用的時(shí)候從池子里面獲取,用完之后不用銷(xiāo)毀,在放到池子里面。這樣不但能節(jié)省內(nèi)存資源,提高性能,而且還能管控線程的總數(shù)量,防止濫用。這時(shí)就需要使用ThreadPool了。
我們來(lái)看看ThreadPool中常用的一些方法。
1、QueueUserWorkItem()
QueueUserWorkItem()方法用來(lái)啟動(dòng)一個(gè)多線程。我們先看看方法的定義:
QueueUserWorkItem()方法有一個(gè)WaitCallback類(lèi)型的參數(shù),在看看WaitCallback的定義:
可以看到WaitCallback就是有一個(gè)object類(lèi)型參數(shù)的委托,所以ThreadPool啟動(dòng)多線程使用下面的代碼:
using System; using System.Threading; namespace ThreadPoolDemo { class Program { static void Main(string[] args) { Console.WriteLine($"start ThreadId: {Thread.CurrentThread.ManagedThreadId.ToString("00")}"); // ThreadPoll啟動(dòng)多線程 ThreadPool.QueueUserWorkItem(p => DoSomethingLong("啟動(dòng)多線程")); Console.WriteLine($"end ThreadId: {Thread.CurrentThread.ManagedThreadId.ToString("00")}"); Console.ReadKey(); } static void DoSomethingLong(string para) { Console.WriteLine($"{para} ThreadId: {Thread.CurrentThread.ManagedThreadId.ToString("00")}"); } } }
運(yùn)行結(jié)果:
2、GetMaxThreads()
GetMaxThreads()用來(lái)獲取線程池中最多可以有多少個(gè)輔助線程和最多有多少個(gè)異步線程。
ThreadPool.GetMaxThreads(out int workerThreads, out int completionPortThreads); Console.WriteLine($"GetMaxThreads workerThreads={workerThreads} completionPortThreads={completionPortThreads}");
程序運(yùn)行結(jié)果:
3、GetMinThreads()
GetMinThreads()用來(lái)獲取線程池中最少可以有多少個(gè)輔助線程和最少有多少個(gè)異步線程。
ThreadPool.GetMinThreads(out int minworkerThreads, out int mincompletionPortThreads); Console.WriteLine($"GetMinThreads workerThreads={minworkerThreads} completionPortThreads={mincompletionPortThreads}");
程序運(yùn)行結(jié)果:
4、SetMaxThreads()和SetMinThreads()
SetMaxThreads()和SetMinThreads()分別用來(lái)設(shè)置線程池中最多線程數(shù)和最少線程數(shù)。
using System; using System.Threading; namespace ThreadPoolDemo { class Program { static void Main(string[] args) { Console.WriteLine($"start ThreadId: {Thread.CurrentThread.ManagedThreadId.ToString("00")}"); // ThreadPoll啟動(dòng)多線程 ThreadPool.QueueUserWorkItem(p => DoSomethingLong("啟動(dòng)多線程")); // 獲取最大線程 ThreadPool.GetMaxThreads(out int workerThreads, out int completionPortThreads); Console.WriteLine($"GetMaxThreads workerThreads={workerThreads} completionPortThreads={completionPortThreads}"); // 獲取最小線程 ThreadPool.GetMinThreads(out int minworkerThreads, out int mincompletionPortThreads); Console.WriteLine($"GetMinThreads workerThreads={minworkerThreads} completionPortThreads={mincompletionPortThreads}"); // 設(shè)置線程池線程 SetThreadPool(); // 輸出設(shè)置后的線程池線程個(gè)數(shù) Console.WriteLine("輸出修改后的最多線程數(shù)和最少線程數(shù)"); ThreadPool.GetMaxThreads(out int maxworkerThreads, out int maxcompletionPortThreads); Console.WriteLine($"GetMaxThreads workerThreads={maxworkerThreads} completionPortThreads={maxcompletionPortThreads}"); ThreadPool.GetMinThreads(out int workerEditThreads, out int completionPortEditThreads); Console.WriteLine($"GetMinThreads workerThreads={workerEditThreads} completionPortThreads={completionPortEditThreads}"); Console.WriteLine($"end ThreadId: {Thread.CurrentThread.ManagedThreadId.ToString("00")}"); Console.ReadKey(); } static void DoSomethingLong(string para) { Console.WriteLine($"{para} ThreadId: {Thread.CurrentThread.ManagedThreadId.ToString("00")}"); } /// <summary> /// 設(shè)置線程池線程個(gè)數(shù) /// </summary> static void SetThreadPool() { Console.WriteLine("************設(shè)置最多線程數(shù)和最少線程數(shù)****************"); // 設(shè)置最大線程 ThreadPool.SetMaxThreads(16, 16); // 設(shè)置最小線程 ThreadPool.SetMinThreads(8, 8); } } }
程序運(yùn)行結(jié)果:
二、線程等待
先來(lái)看下面一個(gè)小例子:
ThreadPool.QueueUserWorkItem(p => DoSomethingLong("啟動(dòng)多線程")); Console.WriteLine("等著QueueUserWorkItem完成后才執(zhí)行");
我們想讓異步多線程執(zhí)行完以后再輸出“等著QueueUserWorkItem完成后才執(zhí)行” 這句話,上面的代碼運(yùn)行效果如下:
從截圖中可以看出,效果并不是我們想要的,Thread中提供了暫停、恢復(fù)等API,但是ThreadPool中沒(méi)有這些API,在ThreadPool中要實(shí)現(xiàn)線程等待,需要使用到ManualResetEvent類(lèi)。
ManualResetEvent類(lèi)的定義如下:
ManualResetEvent需要一個(gè)bool類(lèi)型的參數(shù)來(lái)表示暫停和停止。上面的代碼修改如下:
// 參數(shù)設(shè)置為false ManualResetEvent manualResetEvent = new ManualResetEvent(false); ThreadPool.QueueUserWorkItem(p => { DoSomethingLong("啟動(dòng)多線程"); // 設(shè)置為true manualResetEvent.Set(); }); // manualResetEvent.WaitOne(); Console.WriteLine("等著QueueUserWorkItem完成后才執(zhí)行");
結(jié)果:
ManualResetEvent類(lèi)的參數(shù)值執(zhí)行順序如下:
(1)、false--WaitOne等待--Set--true--WaitOne直接過(guò)去
(2)、true--WaitOne直接過(guò)去--ReSet--false--WaitOne等待
注意:一般情況下,不要阻塞線程池中的線程,因?yàn)檫@樣會(huì)導(dǎo)致一些無(wú)法預(yù)見(jiàn)的錯(cuò)誤。來(lái)看下面的一個(gè)例子:
static void SetWait() { // 設(shè)置最大線程 ThreadPool.SetMaxThreads(16, 16); // 設(shè)置最小線程 ThreadPool.SetMinThreads(8, 8); ManualResetEvent manualResetEvent = new ManualResetEvent(false); for (int i = 0; i < 20; i++) { int k = i; ThreadPool.QueueUserWorkItem(p => { Console.WriteLine(k); if (k < 18) { manualResetEvent.WaitOne(); } else { // 設(shè)為true manualResetEvent.Set(); } }); } if (manualResetEvent.WaitOne()) { Console.WriteLine("沒(méi)有死鎖、、、"); } else { Console.WriteLine("發(fā)生死鎖、、、"); } }
啟動(dòng)20個(gè)線程,如果k小于18就阻塞當(dāng)前的線程,結(jié)果:
從截圖中看出,只執(zhí)行了16個(gè)線程,后面的線程沒(méi)有執(zhí)行,這是為什么呢?因?yàn)槲覀冊(cè)谏厦嬖O(shè)置了線程池中最多可以有16個(gè)線程,當(dāng)16個(gè)線程都阻塞的時(shí)候,會(huì)造成死鎖,所以后面的線程不會(huì)再執(zhí)行了。
三、線程重用
ThreadPool可以很好的實(shí)現(xiàn)線程的重用,這樣就可以減少內(nèi)存的消耗,看下面的代碼:
/// <summary> /// 測(cè)試ThreadPool線程重用 /// </summary> static void ThreadPoolTest() { // 線程重用 ThreadPool.QueueUserWorkItem(t =>DoSomethingLong("ThreadPool")); ThreadPool.QueueUserWorkItem(t =>DoSomethingLong("ThreadPool")); ThreadPool.QueueUserWorkItem(t =>DoSomethingLong("ThreadPool")); ThreadPool.QueueUserWorkItem(t =>DoSomethingLong("ThreadPool")); ThreadPool.QueueUserWorkItem(t =>DoSomethingLong("ThreadPool")); Thread.Sleep(10 * 1000); Console.WriteLine("前面的計(jì)算都完成了。。。。。。。。"); ThreadPool.QueueUserWorkItem(t =>DoSomethingLong("ThreadPool")); ThreadPool.QueueUserWorkItem(t =>DoSomethingLong("ThreadPool")); ThreadPool.QueueUserWorkItem(t =>DoSomethingLong("ThreadPool")); ThreadPool.QueueUserWorkItem(t =>DoSomethingLong("ThreadPool")); ThreadPool.QueueUserWorkItem(t =>DoSomethingLong("ThreadPool")); }
然后在Main方法里面調(diào)用該方法,輸出結(jié)果如下圖所示:
我們?cè)诖a里面總共創(chuàng)建了10個(gè)線程,而結(jié)果里面只有4個(gè)線程ID,這就說(shuō)明ThreadPool可以實(shí)現(xiàn)線程的重用。下面我們?cè)诳纯碩hread是否可以實(shí)現(xiàn)線程的重用,代碼如下:
/// <summary> /// 測(cè)試Thread線程重用 /// </summary> static void ThreadTest() { for (int i = 0; i < 5; i++) { new Thread(() => DoSomethingLong("Threads")).Start(); } Thread.Sleep(10 * 1000); Console.WriteLine("前面的計(jì)算都完成了。。。。。。。。"); for (int i = 0; i < 5; i++) { new Thread(() => DoSomethingLong("btnThreads")).Start(); } }
然后在Main方法里面調(diào)用,輸入結(jié)果如下圖所示:
我們同樣在代碼里面創(chuàng)建了10個(gè)線程,結(jié)果輸出了10個(gè)線程的ID,這就說(shuō)明Thread不能實(shí)現(xiàn)線程的重用。同樣也說(shuō)明THread的效率沒(méi)有ThreadPool高。
程序完整代碼:
using System; using System.Threading; namespace ThreadPoolDemo { class Program { static void Main(string[] args) { Console.WriteLine($"start ThreadId: {Thread.CurrentThread.ManagedThreadId.ToString("00")}"); //// ThreadPoll啟動(dòng)多線程 //ThreadPool.QueueUserWorkItem(p => DoSomethingLong("啟動(dòng)多線程")); //// 獲取最大線程 //ThreadPool.GetMaxThreads(out int workerThreads, out int completionPortThreads); //Console.WriteLine($"GetMaxThreads workerThreads={workerThreads} completionPortThreads={completionPortThreads}"); //// 獲取最小線程 //ThreadPool.GetMinThreads(out int minworkerThreads, out int mincompletionPortThreads); //Console.WriteLine($"GetMinThreads workerThreads={minworkerThreads} completionPortThreads={mincompletionPortThreads}"); //// 設(shè)置線程池線程 //SetThreadPool(); //// 輸出設(shè)置后的線程池線程個(gè)數(shù) //Console.WriteLine("輸出修改后的最多線程數(shù)和最少線程數(shù)"); //ThreadPool.GetMaxThreads(out int maxworkerThreads, out int maxcompletionPortThreads); //Console.WriteLine($"GetMaxThreads workerThreads={maxworkerThreads} completionPortThreads={maxcompletionPortThreads}"); //ThreadPool.GetMinThreads(out int workerEditThreads, out int completionPortEditThreads); //Console.WriteLine($"GetMinThreads workerThreads={workerEditThreads} completionPortThreads={completionPortEditThreads}"); //Console.WriteLine($"end ThreadId: {Thread.CurrentThread.ManagedThreadId.ToString("00")}"); //// 參數(shù)設(shè)置為false //ManualResetEvent manualResetEvent = new ManualResetEvent(false); //ThreadPool.QueueUserWorkItem(p => //{ // DoSomethingLong("啟動(dòng)多線程"); // // 設(shè)置為true // manualResetEvent.Set(); //}); //// //manualResetEvent.WaitOne(); //Console.WriteLine("等著QueueUserWorkItem完成后才執(zhí)行"); // SetWait(); // ThreadPool實(shí)現(xiàn)線程的重用 // ThreadPoolTest(); // Thread ThreadTest(); Console.WriteLine($"end ThreadId: {Thread.CurrentThread.ManagedThreadId.ToString("00")}"); Console.ReadKey(); } static void DoSomethingLong(string para) { Console.WriteLine($"{para} ThreadId: {Thread.CurrentThread.ManagedThreadId.ToString("00")}"); } /// <summary> /// 設(shè)置線程池線程個(gè)數(shù) /// </summary> static void SetThreadPool() { Console.WriteLine("************設(shè)置最多線程數(shù)和最少線程數(shù)****************"); // 設(shè)置最大線程 ThreadPool.SetMaxThreads(16, 16); // 設(shè)置最小線程 ThreadPool.SetMinThreads(8, 8); } static void SetWait() { // 設(shè)置最大線程 ThreadPool.SetMaxThreads(16, 16); // 設(shè)置最小線程 ThreadPool.SetMinThreads(8, 8); ManualResetEvent manualResetEvent = new ManualResetEvent(false); for (int i = 0; i < 20; i++) { int k = i; ThreadPool.QueueUserWorkItem(p => { Console.WriteLine(k); if (k < 18) { manualResetEvent.WaitOne(); } else { // 設(shè)為true manualResetEvent.Set(); } }); } if (manualResetEvent.WaitOne()) { Console.WriteLine("沒(méi)有死鎖、、、"); } else { Console.WriteLine("發(fā)生死鎖、、、"); } } /// <summary> /// 測(cè)試ThreadPool線程重用 /// </summary> static void ThreadPoolTest() { // 線程重用 ThreadPool.QueueUserWorkItem(t => DoSomethingLong("ThreadPool")); ThreadPool.QueueUserWorkItem(t => DoSomethingLong("ThreadPool")); ThreadPool.QueueUserWorkItem(t => DoSomethingLong("ThreadPool")); ThreadPool.QueueUserWorkItem(t => DoSomethingLong("ThreadPool")); ThreadPool.QueueUserWorkItem(t => DoSomethingLong("ThreadPool")); Thread.Sleep(10 * 1000); Console.WriteLine("前面的計(jì)算都完成了。。。。。。。。"); ThreadPool.QueueUserWorkItem(t => DoSomethingLong("ThreadPool")); ThreadPool.QueueUserWorkItem(t => DoSomethingLong("ThreadPool")); ThreadPool.QueueUserWorkItem(t => DoSomethingLong("ThreadPool")); ThreadPool.QueueUserWorkItem(t => DoSomethingLong("ThreadPool")); ThreadPool.QueueUserWorkItem(t => DoSomethingLong("ThreadPool")); } /// <summary> /// 測(cè)試Thread線程重用 /// </summary> static void ThreadTest() { for (int i = 0; i < 5; i++) { new Thread(() => DoSomethingLong("Threads")).Start(); } Thread.Sleep(10 * 1000); Console.WriteLine("前面的計(jì)算都完成了。。。。。。。。"); for (int i = 0; i < 5; i++) { new Thread(() => DoSomethingLong("btnThreads")).Start(); } } } }
到此這篇關(guān)于C#多線程之線程池ThreadPool用法的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用C#編寫(xiě)兩個(gè)漂亮?xí)r鐘的示例代碼
這篇文章主要為大家分享了兩個(gè)使用C#編寫(xiě)的兩個(gè)漂亮?xí)r鐘的示例代碼,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的可以了解一下2023-07-07Unity實(shí)現(xiàn)菜品識(shí)別的示例代碼
這篇文章主要介紹了如何通過(guò)Unity實(shí)現(xiàn)菜品識(shí)別,可以準(zhǔn)確識(shí)別圖片中的菜品名稱(chēng)、位置、卡路里信息,并獲取百科信息。感興趣的小伙伴可以了解一下2022-02-02C#實(shí)現(xiàn)生成mac地址與IP地址注冊(cè)碼的兩種方法
這篇文章主要介紹了C#實(shí)現(xiàn)生成mac地址與IP地址注冊(cè)碼的兩種方法,非常實(shí)用的技巧,需要的朋友可以參考下2014-09-09Unity使用LineRender實(shí)現(xiàn)繪畫(huà)功能
這篇文章主要為大家詳細(xì)介紹了Unity使用LineRender實(shí)現(xiàn)繪畫(huà)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07解決C#中Linq GroupBy 和OrderBy失效的方法
最近發(fā)現(xiàn)了一個(gè)問(wèn)題,在服務(wù)器端的Linq GroupBy 和OrderBy居然不管用,后來(lái)終于解決了所以現(xiàn)在分享給大家,有需要的朋友們可以參考借鑒。2016-09-09如何讓C#、VB.NET實(shí)現(xiàn)復(fù)雜的二進(jìn)制操作
VB.NET和C#屬于高級(jí)語(yǔ)言,對(duì)二進(jìn)制位操作的支持不是很好,比如沒(méi)有了移位運(yùn)算等,用的時(shí)候確實(shí)很不方便,所以在閑暇之余我重新封裝了一個(gè)用于C#、VB.NET的位操作類(lèi)庫(kù),通過(guò)該類(lèi)庫(kù)可以實(shí)現(xiàn)數(shù)據(jù)移位、循環(huán)移位、轉(zhuǎn)換為二進(jìn)制、將二進(jìn)制轉(zhuǎn)換為數(shù)據(jù)等2013-07-07WPF中使用WebView2控件的方法及常見(jiàn)問(wèn)題
WebView2為WPF網(wǎng)頁(yè)瀏覽工具,具有簡(jiǎn)單易用,頁(yè)面顯示清晰的優(yōu)點(diǎn),下面這篇文章主要給大家介紹了關(guān)于WPF中使用WebView2控件的方法及常見(jiàn)問(wèn)題,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02