.NET線程池

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Text;   
  4. using System.Collections;   
  5. using System.Threading;   
  6. using System.Net.Sockets;   
  7.   
  8. namespace SpiderClient   
  9. {   
  10.     class ThreadPool   
  11.     {   
  12.   
  13.         //本客戶度連接sockt   
  14.   
  15.         private Client c = null;   
  16.   
  17.         //線程池中數量   
  18.         private int threadcount;   
  19.   
  20.         //保存所有線程池對象   
  21.         private ArrayList threads = null;   
  22.   
  23.         //保存所有mywebrequest對象   
  24.         private ArrayList requests = null;   
  25.   
  26.         //線程時候停止 可以外部控制   
  27.         public bool isstopthread = false;   
  28.   
  29.         public ThreadPool(Client _c,int _threadcount)   
  30.         {   
  31.             //   
  32.             c = _c;   
  33.             threadcount = _threadcount;   
  34.             threads = new ArrayList();   
  35.             requests = new ArrayList();   
  36.   
  37.         }   
  38.   
  39.         public static ThreadPool CreateThreadPool(ThreadPool tp, Client _c, int _threadcount)   
  40.         {   
  41.             if (tp == null)   
  42.             {   
  43.                 return new ThreadPool(_c, _threadcount);   
  44.             }   
  45.             else  
  46.             {   
  47.                 tp.threads.Clear();   
  48.                 tp.requests.Clear();   
  49.   
  50.                 return tp;   
  51.             }   
  52.         }   
  53.   
  54.         public void StartPool()   
  55.         {   
  56.             Thread t = null;   
  57.             MyWebRequest mwr = null;   
  58.             for (int i = 0; i < threadcount; i++)   
  59.             {   
  60.                 t = new Thread(new ParameterizedThreadStart(ThreadFunc));   
  61.                 requests.Add(mwr);   
  62.                 threads.Add(t);   
  63.                 t.Start(i);   
  64.             }   
  65.         }   
  66.   
  67.   
  68.         public void StopAllThreads()   
  69.         {   
  70.             isstopthread = true;   
  71.             for (int i = 0; i < threadcount; i++)   
  72.             {   
  73.                 Thread t = (Thread)threads[i];   
  74.                 if (t != null)   
  75.                 {   
  76.                     if (t.ThreadState == ThreadState.Suspended)   
  77.                     {   
  78.                         t.Start();   
  79.                     }   
  80.                     t.Abort();   
  81.                 }   
  82.             }   
  83.         }   
  84.   
  85.         private void ThreadFunc(object o)   
  86.         {   
  87.             int i = (int)o;   
  88.             while (!isstopthread)   
  89.             {   
  90.                 Thread.Sleep(100);   
  91.             }   
  92.         }   
  93.     }   
  94. }   

=========================================================================

簡單代碼如下:

 
  1. using System;   
  2. using System.Threading;   
  3. public class Example {   
  4.     public static void Main() {   
  5.         // Queue the task.   
  6.         ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));   
  7.            
  8.         Console.WriteLine("Main thread does some work, then sleeps.");   
  9.         // If you comment out the Sleep, the main thread exits before   
  10.         // the thread pool task runs.  The thread pool uses background   
  11.         // threads, which do not keep the application running.  (This   
  12.         // is a simple example of a race condition.)   
  13.         Thread.Sleep(1000);   
  14.   
  15.         Console.WriteLine("Main thread exits.");   
  16.     }   
  17.   
  18.     // This thread procedure performs the task.   
  19.     static void ThreadProc(Object stateInfo) {   
  20.         // No state object was passed to QueueUserWorkItem, so    
  21.         // stateInfo is null.   
  22.         Console.WriteLine("Hello from the thread pool.");   
  23.     }   
  24. }   



另外一個例子是關於自定義數據參與線程池處理的代碼:

 
  1. using System;   
  2. using System.Threading;   
  3.   
  4. // TaskInfo holds state information for a task that will be   
  5. // executed by a ThreadPool thread.   
  6. public class TaskInfo {   
  7.     // State information for the task.  These members   
  8.     // can be implemented as read-only properties, read/write   
  9.     // properties with validation, and so on, as required.   
  10.     public string Boilerplate;   
  11.     public int Value;   
  12.   
  13.     // Public constructor provides an easy way to supply all   
  14.     // the information needed for the task.   
  15.     public TaskInfo(string text, int number) {   
  16.         Boilerplate = text;   
  17.         Value = number;   
  18.     }   
  19. }   
  20.   
  21. public class Example {   
  22.     public static void Main() {   
  23.         // Create an object containing the information needed   
  24.         // for the task.   
  25.         TaskInfo ti = new TaskInfo("This report displays the number {0}.", 42);   
  26.   
  27.         // Queue the task and data.   
  28.         if (ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), ti)) {       
  29.             Console.WriteLine("Main thread does some work, then sleeps.");   
  30.   
  31.             // If you comment out the Sleep, the main thread exits before   
  32.             // the ThreadPool task has a chance to run.  ThreadPool uses    
  33.             // background threads, which do not keep the application    
  34.             // running.  (This is a simple example of a race condition.)   
  35.             Thread.Sleep(1000);   
  36.   
  37.             Console.WriteLine("Main thread exits.");   
  38.         }   
  39.         else {   
  40.             Console.WriteLine("Unable to queue ThreadPool request.");    
  41.         }   
  42.     }   
  43.   
  44.     // The thread procedure performs the independent task, in this case   
  45.     // formatting and printing a very simple report.   
  46.     //   
  47.     static void ThreadProc(Object stateInfo) {   
  48.         TaskInfo ti = (TaskInfo) stateInfo;   
  49.         Console.WriteLine(ti.Boilerplate, ti.Value);    
  50.     }   
  51. }   
發佈了23 篇原創文章 · 獲贊 4 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章