C# ThreadPool之QueueUserWorkItem使用案例詳解
先看代碼:
//設(shè)置可以同時處于活動狀態(tài)的線程池的請求數(shù)目。 bool pool = ThreadPool.SetMaxThreads(8, 8); if (pool) { ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)1")); ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)2")); ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)3")); ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)4")); ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)5")); ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)6")); ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)7")); ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)8")); ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)9")); ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)10")); ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)11")); };
上面代碼先設(shè)置線程池中最大并發(fā)量為8個,然后通過QueueUserWorkItem向線程池中添加11個方法,運(yùn)行,輸出結(jié)果:
可以看出,先運(yùn)行了8個,當(dāng)有一個任務(wù)結(jié)束后線程池中有空閑線程時,排隊(duì)的下一個任務(wù)才會執(zhí)行,
把最大并發(fā)量改成9試試:
{ //設(shè)置可以同時處于活動狀態(tài)的線程池的請求數(shù)目。 bool pool = ThreadPool.SetMaxThreads(9, 9); if (pool) { ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)1")); ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)2")); ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)3")); ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)4")); ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)5")); ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)6")); ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)7")); ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)8")); ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)9")); ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)10")); ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("參數(shù)11")); }; }
運(yùn)行結(jié)果:
果然沒錯,這次是先執(zhí)行9個,當(dāng)有空閑線程時再執(zhí)行下一個
總結(jié)一下
QueueUserWorkItem:將方法排入隊(duì)列以便執(zhí)行。 此方法在有線程池線程變得可用時執(zhí)行。
到此這篇關(guān)于C# ThreadPool之QueueUserWorkItem使用案例詳解的文章就介紹到這了,更多相關(guān)C# ThreadPool之QueueUserWorkItem內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)的SN快速輸入工具實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的SN快速輸入工具,以實(shí)例的形式詳細(xì)講述了C#實(shí)現(xiàn)序列號快速輸入的方法,是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11C#中IEnumerable、ICollection、IList、List之間的區(qū)別
這篇文章主要介紹了C#中IEnumerable、ICollection、IList、List之間的區(qū)別,本文分別分析了它的實(shí)現(xiàn)源碼,從而總結(jié)出了它們之間的關(guān)系和不同之處,需要的朋友可以參考下2015-06-06C#從windows剪貼板獲取并顯示文本內(nèi)容的方法
這篇文章主要介紹了C#從windows剪貼板獲取并顯示文本內(nèi)容的方法,涉及C#操作剪貼板的相關(guān)技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-04-04c#操作sql server2008 的界面實(shí)例代碼
這篇文章主要介紹了c#操作sql server2008 的界面實(shí)例代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03淺析C#中數(shù)組,ArrayList與List對象的區(qū)別
在C#中,當(dāng)我們想要存儲一組對象的時候,就會想到用數(shù)組,ArrayList,List這三個對象了。那么這三者到底有什么樣的區(qū)別呢2013-07-07C#保存listbox中數(shù)據(jù)到文本文件的方法
這篇文章主要介紹了C#保存listbox中數(shù)據(jù)到文本文件的方法,涉及C#操作listbox數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2015-04-04