原文來自Smart Thread Pool,這篇文章很長(感覺不是一篇文章,而是一本書),文章描敘爲:A .Net Thread Pool fully implement in C# with many features。
文中作者整合/改進了別人的線程池,變成我們現在看到的智能線程池(Smart Thread Pool,類庫版本.Net 2.0)。它具有以下特徵:
1)線程池中線程的數量根據工作量的變化而改變;
2)Work Items可以返回一個值;
3)如果Work Item沒有開始執行,那它可以被取消的;
4)當work Item被執行時,調用線程的上下文(context)可以被使用的(不是很明白?);
5)使用最小數量的Window32事件句柄,可以保證在應用中句柄的數量不會大幅擴大(也就是使用系統資源比較少);
6)調用者可以等待多個或所有的WorkItem完成(這個很有用的);
7)一個Work Item可以有PostExecute的回掉函數,它主要用於當Work Item完成後開始被執行;
8)伴隨Work Item的狀態對象(state Object)可以被自動釋放;
9)Work Item運行過程中的異常是被返回到調用者;
10)Work Item是有優先級的;
11)Work Item有組的區分;
12)調用者可以暫停線程池和Work Item組的開始;
13)線程也有優先級;
14)線程有初始化及結束事件;
15)支持winCE平臺(沒玩過);
16)支持Action<T>和Func<T>泛型方法(也不是很明白);
17)<t>SilverLight(Web前端應用開發)也支持;
18)Mono(.Net平臺下混合編程語言)也支持;
19)<t>Performance Counters (Windows and internal);
20)WorkItem支持超時(主動);
21)線程ApartmentState;
22)線程是否爲後臺線程;
23)線程名字模板;
24)支持Windowns Phone(受限);
25)可以設定線程的MaxStackSize;
接下來我們通過實例代碼來熟悉這個智能線程池。爲了簡化,通過控制檯來直接調用。
CatchException:
public class CatchExceptionExample
{
public void DoWork()
{
SmartThreadPool smartThreadPool = new SmartThreadPool();
//the 3rd param double is the result
IWorkItemResult<double> wir = smartThreadPool.QueueWorkItem(new Func<double, double, double>(DoDiv), 10.0, 0.0);
try
{
double result = wir.Result;
Console.WriteLine(result.ToString());
}
// Catch the exception that Result threw
catch (WorkItemResultException e)
{
// Dump the inner exception which DoDiv threw
Debug.WriteLine(e.InnerException);
}
smartThreadPool.Shutdown();
}
private double DoDiv(double x, double y)
{
return x / y;
}
}
控制檯輸出的結果:
這印證了上面的第九條:Work Item返回的異常會返回到調用者。實際調用時是這樣表達的:
public static void Main()
{
CatchExceptionExample catchExc1 = new CatchExceptionExample();
catchExc1.DoWork();
Console.ReadKey();
}
CooperativeCancel:
public class CooperativeCancelExample
{
public void DoWork(object state)
{
SmartThreadPool smartThreadPool = new SmartThreadPool();
// Queue the work item
IWorkItemResult wir = smartThreadPool.QueueWorkItem(DoRealWork);
// Give the work item some time to complete.
Thread.Sleep(1000);
// If the work item hasn't completed yet then cancel it.
if (!wir.IsCompleted)
{
Console.WriteLine("Work item canceling started");
wir.Cancel();
Console.WriteLine("Work item canceling ended");
}
smartThreadPool.Shutdown();
}
// Do some lengthy work
private void DoRealWork()
{
Console.WriteLine("In DoRealWork()");
Random rm = new Random();
Thread.Sleep(1);
int time = rm.Next(10, 30);
// Do something here.
Thread.Sleep(time);
Console.WriteLine("Sleep before do real job,time=" + time.ToString());
// Sample SmartThreadPool.IsWorkItemCanceled
if (SmartThreadPool.IsWorkItemCanceled)
{
Console.WriteLine("Work Item canceled, return directly");
return;
}
// Sample the SmartThreadPool.IsWorkItemCanceled in a loop
while (!SmartThreadPool.IsWorkItemCanceled)
{
// Do some real work here
Thread.Sleep(1);
time = rm.Next(1000, 3000);
Thread.Sleep(time);
Console.WriteLine("Work Item do real job, time=" + time.ToString());
}
}
}
控制檯輸出的結果:
結果顯示:已經進入到DoRealWork()中,由於沒有結束,所以可以取消,對應上面的第三項。
GetException:
public class GetExceptionExample
{
private class DivArgs
{
public int x;
public int y;
}
public void DoWork()
{
SmartThreadPool smartThreadPool = new SmartThreadPool();
DivArgs divArgs = new DivArgs();
divArgs.x = 10;
divArgs.y = 0;
IWorkItemResult wir = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoDiv), divArgs);
Exception e;
object obj = wir.GetResult(out e);
// e contains the expetion that DoDiv threw
if(null == e)
{
int result = (int)obj;
Console.WriteLine("No exception, got result= " + result.ToString());
}
else
{
// Do something with the exception
Console.WriteLine("Got exception in result: " + e.ToString());
}
smartThreadPool.Shutdown();
}
private object DoDiv(object state)
{
DivArgs divArgs = (DivArgs)state;
return (divArgs.x / divArgs.y);
}
}
控制檯輸出結果:
同樣對應上面的第九項,對線程池中的結果進行檢查,Exception也包含在結果中,區分異常e的方式是通過判斷其是否爲空。