本文档转自http://blog.csdn.net/greystar/archive/2007/08/31/1766363.aspx。
源码下载http://www.codeproject.com/KB/threads/smartthreadpool.aspx。

顾名思义,智能线程池.一定比自带的线程池有过人之处.不然也没有必要再搞个出来了.
- 可创建线程池实例。
- 可动态调整线程池工作线程数量。
- WorkItem 可以返回信息。
- 未执行 WorkItem 可被取消。
- WorkItem 执行时可使用调用者上下文。
- 调用者可等待多个或全部 WorkItem 执行结束。
- WorkItem 允许拥有一个执行结束时被执行的 PostExecute 回调委托。
- 可以向 WorkItem 传递一个状态对象,并且会在执行结束时自动调用 IDisposable.Dispose()。
- WorkItem 异常会传递给调用者。
- 支持 WorkItem 分组。
- 可挂起线程池或分组。
- 可以设置 WorkItem 优先级。
- 可以设置线程优先级。
相比较而言,System.Threading.ThreadPool 只适合做些简单的工作。(本来也是为了异步委托而实现的)
1. 简单示例
代码
1 static void Main(string[] args)
2 {
3 SmartThreadPool smart = new SmartThreadPool();
4 smart.Start();
5
6 IWorkItemResult result = smart.QueueWorkItem(delegate(object state)
7 {
8 Console.WriteLine("Thread:{0}; State:{1}", Thread.CurrentThread.ManagedThreadId, state);
9 return DateTime.Now;
10 }, 123);
11
12 SmartThreadPool.WaitAll(new IWorkItemResult[] { result });
13 Console.WriteLine(result.Result);
14
15 smart.Shutdown();
16 }
2 {
3 SmartThreadPool smart = new SmartThreadPool();
4 smart.Start();
5
6 IWorkItemResult result = smart.QueueWorkItem(delegate(object state)
7 {
8 Console.WriteLine("Thread:{0}; State:{1}", Thread.CurrentThread.ManagedThreadId, state);
9 return DateTime.Now;
10 }, 123);
11
12 SmartThreadPool.WaitAll(new IWorkItemResult[] { result });
13 Console.WriteLine(result.Result);
14
15 smart.Shutdown();
16 }
输出:
Thread:11; State:123
2007-8-9 12:42:51
2. 参数设置
代码
1 STPStartInfo stp = new STPStartInfo();
2 stp.DisposeOfStateObjects = true;
3 stp.CallToPostExecute = CallToPostExecute.Always;
4 stp.ThreadPriority = ThreadPriority.Highest;
5 stp.UseCallerCallContext = true;
6 stp.MaxWorkerThreads = 20;
7
8 SmartThreadPool smart = new SmartThreadPool(stp);
9 smart.Start();
10
11 IWorkItemResult result = smart.QueueWorkItem(delegate(object state)
12 {
13 return DateTime.Now;
14 }, null);
15
16 smart.WaitForIdle();
17 smart.Shutdown();
2 stp.DisposeOfStateObjects = true;
3 stp.CallToPostExecute = CallToPostExecute.Always;
4 stp.ThreadPriority = ThreadPriority.Highest;
5 stp.UseCallerCallContext = true;
6 stp.MaxWorkerThreads = 20;
7
8 SmartThreadPool smart = new SmartThreadPool(stp);
9 smart.Start();
10
11 IWorkItemResult result = smart.QueueWorkItem(delegate(object state)
12 {
13 return DateTime.Now;
14 }, null);
15
16 smart.WaitForIdle();
17 smart.Shutdown();
3. 自动释放 State
如果 State 对象实现了 IDisposable 接口,那么我们可以通过设置线程池参数,当 WorkItem 结束时自动调用 state.Dispose()。
代码
1 class State : IDisposable
2 {
3 public void Dispose()
4 {
5 Console.WriteLine("Dispose...");
6 }
7 }
8
9 public class Program
10 {
11 static void Main(string[] args)
12 {
13 STPStartInfo stp = new STPStartInfo();
14 stp.DisposeOfStateObjects = true;
15
16 SmartThreadPool smart = new SmartThreadPool(stp);
17 smart.Start();
18
19 IWorkItemResult result = smart.QueueWorkItem(delegate(object state)
20 {
21 return DateTime.Now;
22 }, new State());
23
24 SmartThreadPool.WaitAll(new IWorkItemResult[] { result });
25 Console.WriteLine(result.Result);
26
27 smart.Shutdown();
28 }
29 }
2 {
3 public void Dispose()
4 {
5 Console.WriteLine("Dispose...");
6 }
7 }
8
9 public class Program
10 {
11 static void Main(string[] args)
12 {
13 STPStartInfo stp = new STPStartInfo();
14 stp.DisposeOfStateObjects = true;
15
16 SmartThreadPool smart = new SmartThreadPool(stp);
17 smart.Start();
18
19 IWorkItemResult result = smart.QueueWorkItem(delegate(object state)
20 {
21 return DateTime.Now;
22 }, new State());
23
24 SmartThreadPool.WaitAll(new IWorkItemResult[] { result });
25 Console.WriteLine(result.Result);
26
27 smart.Shutdown();
28 }
29 }
4. PostExecute CallBack
该委托在 WorkItem 结束时被调用。
代码
1 SmartThreadPool smart = new SmartThreadPool();
2 smart.Start();
3
4 IWorkItemResult result = smart.QueueWorkItem(delegate(object state)
5 {
6 return DateTime.Now;
7 }, null, delegate(IWorkItemResult _result)
8 {
9 Console.WriteLine(_result.Result);
10 });
11
2 smart.Start();
3
4 IWorkItemResult result = smart.QueueWorkItem(delegate(object state)
5 {
6 return DateTime.Now;
7 }, null, delegate(IWorkItemResult _result)
8 {
9 Console.WriteLine(_result.Result);
10 });
11
5. Work Items Group
根据使用目的不同,我们可以将线程池划分为多个组。针对组进行管理,显然要比创建多个线程池对象要好些。
代码
1 SmartThreadPool smart = new SmartThreadPool();
2 smart.Start();
3
4 IWorkItemsGroup group = smart.CreateWorkItemsGroup(3);
5 IWorkItemResult result = group.QueueWorkItem(delegate(object state)
6 {
7 return DateTime.Now;
8 }, null, delegate(IWorkItemResult _result)
9 {
10 Console.WriteLine(_result.Result);
11 });
12
13 group.WaitForIdle();
14 smart.Shutdown();
2 smart.Start();
3
4 IWorkItemsGroup group = smart.CreateWorkItemsGroup(3);
5 IWorkItemResult result = group.QueueWorkItem(delegate(object state)
6 {
7 return DateTime.Now;
8 }, null, delegate(IWorkItemResult _result)
9 {
10 Console.WriteLine(_result.Result);
11 });
12
13 group.WaitForIdle();
14 smart.Shutdown();
更多细节可参考作者原文档。
附:作者是个有心人,这个组件和文档一直持续升级和维护,在此表示感谢。

浙公网安备 33010602011771号