欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

C#多線程ThreadPool線程池詳解

簡單說明一下:

"熱情、務(wù)實(shí)、專業(yè)、創(chuàng)新”我們不忘初心,砥礪前行,實(shí)在做人,認(rèn)真做事,始終如一的專注企業(yè)互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營銷推廣服務(wù)。成都品牌網(wǎng)站建設(shè)本著讓客戶滿意的目標(biāo),幫助企業(yè)通過互聯(lián)網(wǎng)創(chuàng)造價(jià)值.創(chuàng)新互聯(lián)公司全體員工將通過不懈努力,力爭成為客戶在信息化領(lǐ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é)果:

C#多線程ThreadPool線程池詳解

使用例子二:

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é)果:

C#多線程ThreadPool線程池詳解

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

文章名稱:C#多線程ThreadPool線程池詳解
轉(zhuǎn)載來源:http://www.chinadenli.net/article30/gghipo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器外貿(mào)建站電子商務(wù)Google網(wǎng)站設(shè)計(jì)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司