国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#多線程ThreadPool線程池詳解

2020-01-24 00:35:26
字體:
供稿:網(wǎng)友

簡單說明一下:

線程池可以看做容納線程的容器;一個(gè)應(yīng)用程序最多只能有一個(gè)線程池;ThreadPool靜態(tài)類通過QueueUserWorkItem()方法將工作函數(shù)排入線程池; 每排入一個(gè)工作函數(shù),就相當(dāng)于請求創(chuàng)建一個(gè)線程;

線程池的作用:

1、線程池是為突然大量爆發(fā)的線程設(shè)計(jì)的,通過有限的幾個(gè)固定線程為大量的操作服務(wù),減少了創(chuàng)建和銷毀線程所需的時(shí)間,從而提高效率。
2、如果一個(gè)線程的時(shí)間非常長,就沒必要用線程池了(不是不能作長時(shí)間操作,而是不宜。),況且我們還不能控制線程池中線程的開始、掛起、和中止。

一些使用例子:

實(shí)例一:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ThreadPoolDemo{ class Program {  static void Main(string[] args)  {   System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(TestThreadPool),new string[] {"drsw","sfs","sdfs"});   Console.ReadKey();  }  public static void TestThreadPool(object state)  {   string[] arry = state as string[]; //傳過來的參數(shù)值   int workerThreads = 0;   int completionPortThreads = 0;   System.Threading.ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);   Console.Write(DateTime.Now.ToString() + "--" + arry[0] + "----workerThreads=" + workerThreads + "----completionPortThreads=" + completionPortThreads);  } }}

上述代碼運(yùn)行結(jié)果:

使用例子二:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ThreadPoolDemo{ class Program {  static void Main(string[] args)  {   System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(TestThreadPool));   Console.ReadKey();  }  public static void TestThreadPool(object state)  {   int workerThreads = 0;   int completionPortThreads = 0;   System.Threading.ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);   Console.Write(DateTime.Now.ToString() + "----workerThreads=" + workerThreads + "----completionPortThreads=" + completionPortThreads);  } }}

上述代碼運(yùn)行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 丹寨县| 从化市| 高密市| 利津县| 阳春市| 丽水市| 孟州市| 荃湾区| 项城市| 金山区| 贵阳市| 博客| 遂川县| 体育| 景洪市| 德昌县| 沈丘县| 彰化县| 广饶县| 洛浦县| 巫溪县| 扬州市| 孝感市| 东光县| 沂南县| 富川| 宝兴县| 崇阳县| 嘉鱼县| 舒城县| 莱西市| 濮阳市| 饶平县| 绩溪县| 大冶市| 伊宁市| 北流市| 祥云县| 攀枝花市| 苏尼特右旗| 三河市|