C#多線程ThreadPool線程池詳解
簡(jiǎn)單說(shuō)明一下:
線程池可以看做容納線程的容器;一個(gè)應(yīng)用程序最多只能有一個(gè)線程池;ThreadPool靜態(tài)類通過(guò)QueueUserWorkItem()方法將工作函數(shù)排入線程池; 每排入一個(gè)工作函數(shù),就相當(dāng)于請(qǐng)求創(chuàng)建一個(gè)線程;
線程池的作用:
1、線程池是為突然大量爆發(fā)的線程設(shè)計(jì)的,通過(guò)有限的幾個(gè)固定線程為大量的操作服務(wù),減少了創(chuàng)建和銷毀線程所需的時(shí)間,從而提高效率。
2、如果一個(gè)線程的時(shí)間非常長(zhǎng),就沒(méi)必要用線程池了(不是不能作長(zhǎng)時(shí)間操作,而是不宜。),況且我們還不能控制線程池中線程的開(kāi)始、掛起、和中止。
一些使用例子:
實(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[]; //傳過(guò)來(lái)的參數(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é)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# Winform中DataGridView導(dǎo)出為Excel的實(shí)現(xiàn)示例
本文主要介紹了C# Winform中DataGridView導(dǎo)出為Excel的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
C#使用Dispose模式實(shí)現(xiàn)手動(dòng)對(duì)資源的釋放
這篇文章主要介紹了C#使用Dispose模式實(shí)現(xiàn)手動(dòng)對(duì)資源的釋放,涉及C#采用Dispose模式操作資源的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
C#使用FtpWebRequest與FtpWebResponse完成FTP操作
這篇文章介紹了C#使用FtpWebRequest與FtpWebResponse完成FTP操作的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
C#使用Directoryinfo類獲得目錄信息和屬性的方法
這篇文章主要介紹了C#使用Directoryinfo類獲得目錄信息和屬性的方法,涉及C#操作目錄的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
c#的時(shí)間日期操作示例分享(c#獲取當(dāng)前日期)
這篇文章主要介紹了c#的時(shí)間日期操作示例,在給定時(shí)間戳返回指定的時(shí)間格式和獲取當(dāng)前時(shí)間方法,需要的朋友可以參考下2014-03-03
c#用Treeview實(shí)現(xiàn)FolderBrowerDialog 和動(dòng)態(tài)獲取系統(tǒng)圖標(biāo)(運(yùn)用了Win32 
其實(shí),FolderBrowerDialog 很好用呢,有木有啊親,反正我特別的喜歡,微軟大哥把這個(gè)瀏覽文件夾的東東封裝的多好呀2013-03-03
C#使用委托(delegate)實(shí)現(xiàn)在兩個(gè)form之間傳遞數(shù)據(jù)的方法
這篇文章主要介紹了C#使用委托(delegate)實(shí)現(xiàn)在兩個(gè)form之間傳遞數(shù)據(jù)的方法,涉及C#委托的使用技巧,需要的朋友可以參考下2015-04-04

