C#---线程池学习总结(未完。。。。)

来源:互联网 发布:永久域名发布网器 编辑:程序博客网 时间:2024/05/22 06:33

1.当程序创建新的线程时需要大量的开销,而Windows操作系统允许用户维持一池的”预先建立“的线程。

2.线程池为程序中指定的方法提供工作线程,有一个特定的线程控制着线程池的工作,但是应用程序也可以分配附加的线程去控制线程池。

Code:
  1. 3. BindHandle(safehandle)//  将操作系统的句柄绑定到线程池                
  2.   
  3.   GetAvailableThreads(out int workersThreads , out int CompletionworkersThreads)//得到当前线程池中可用线程的数量   
  4.   QueueUserWorkItem()//在线程池中查询用户代表   
  5. /**********************************注册代表********************************************/  
  6.     ThreadPool.QueueUserWorkerItem( new WaitCallBack(Func1));   
  7. /**********************************注册代表********************************************/  

3,学习线程池的例子

Code:
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Text;   
  5. using System.Threading;   
  6. namespace 线程池   
  7. {   
  8.     class ThreadPoolSample   
  9.     {   
  10.         public ThreadPoolSample()   
  11.         {   
  12.             int i;   
  13.             ThreadPool.QueueUserWorkItem(new WaitCallback(Func1));   
  14.             ThreadPool.QueueUserWorkItem(new WaitCallback(Func2));   
  15.             for (i = 0; i < 10; i++)   
  16.             {   
  17.                 Console.WriteLine("Main:{0}", i);   
  18.                 Thread.Sleep(1000);   
  19.             }   
  20.         }   
  21.         void Func1(object state)   
  22.         {   
  23.             int i;   
  24.             for (i = 0; i < 10; i++)   
  25.             {   
  26.                 Console.WriteLine("Thread1:{0}", i);   
  27.                 Thread.Sleep(2000);   
  28.             }   
  29.         }   
  30.         void Func2(object state)   
  31.         {   
  32.             int i;   
  33.             for (i = 0; i < 10; i++)   
  34.             {   
  35.                 Console.WriteLine("Thread2:{0}", i);   
  36.                 Thread.Sleep(3000);   
  37.             }   
  38.         }   
  39.         static void Main(string[] args)   
  40.         {   
  41.             ThreadPoolSample myThreadPoolSample = new ThreadPoolSample();   
  42.   
  43.         }   
  44.     }   
  45. }   

http://hi.csdn.net/Achillse_XuMian

http://hi.baidu.com/achillse_yh