By Ami Bar. A smart Thread Pool implementation in .NET.
http://www.codeproject.com/cs/threads/smartthreadpool.asp
顾名思义,智能线程池.一定比自带的线程池有过人之处.不然也没有必要再搞个出来了.
- 可创建线程池实例。
- 可动态调整线程池工作线程数量。
- WorkItem 可以返回信息。
- 未执行 WorkItem 可被取消。
- WorkItem 执行时可使用调用者上下文。
- 调用者可等待多个或全部 WorkItem 执行结束。
- WorkItem 允许拥有一个执行结束时被执行的 PostExecute 回调委托。
- 可以向 WorkItem 传递一个状态对象,并且会在执行结束时自动调用 IDisposable.Dispose()。
- WorkItem 异常会传递给调用者。
- 支持 WorkItem 分组。
- 可挂起线程池或分组。
- 可以设置 WorkItem 优先级。
- 可以设置线程优先级。
1. 简单示例
static void Main(string[] args)
{
SmartThreadPool smart = new SmartThreadPool();
smart.Start();
IWorkItemResult result = smart.QueueWorkItem(delegate(object state)
{
Console.WriteLine("Thread:{0}; State:{1}", Thread.CurrentThread.ManagedThreadId, state);
return DateTime.Now;
}, 123);
SmartThreadPool.WaitAll(new IWorkItemResult[] { result });
Console.WriteLine(result.Result);
smart.Shutdown();
}
{
SmartThreadPool smart = new SmartThreadPool();
smart.Start();
IWorkItemResult result = smart.QueueWorkItem(delegate(object state)
{
Console.WriteLine("Thread:{0}; State:{1}", Thread.CurrentThread.ManagedThreadId, state);
return DateTime.Now;
}, 123);
SmartThreadPool.WaitAll(new IWorkItemResult[] { result });
Console.WriteLine(result.Result);
smart.Shutdown();
}
输出:
Thread:11; State:123
2007-8-9 12:42:51
2. 参数设置
STPStartInfo stp = new STPStartInfo();
stp.DisposeOfStateObjects = true;
stp.CallToPostExecute = CallToPostExecute.Always;
stp.ThreadPriority = ThreadPriority.Highest;
stp.UseCallerCallContext = true;
stp.MaxWorkerThreads = 20;
SmartThreadPool smart = new SmartThreadPool(stp);
smart.Start();
IWorkItemResult result = smart.QueueWorkItem(delegate(object state)
{
return DateTime.Now;
}, null);
smart.WaitForIdle();
smart.Shutdown();
stp.DisposeOfStateObjects = true;
stp.CallToPostExecute = CallToPostExecute.Always;
stp.ThreadPriority = ThreadPriority.Highest;
stp.UseCallerCallContext = true;
stp.MaxWorkerThreads = 20;
SmartThreadPool smart = new SmartThreadPool(stp);
smart.Start();
IWorkItemResult result = smart.QueueWorkItem(delegate(object state)
{
return DateTime.Now;
}, null);
smart.WaitForIdle();
smart.Shutdown();
3. 自动释放 State
如果 State 对象实现了 IDisposable 接口,那么我们可以通过设置线程池参数,当 WorkItem 结束时自动调用 state.Dispose()。
class State : IDisposable
{
public void Dispose()
{
Console.WriteLine("Dispose...");
}
}
public class Program
{
static void Main(string[] args)
{
STPStartInfo stp = new STPStartInfo();
stp.DisposeOfStateObjects = true;
SmartThreadPool smart = new SmartThreadPool(stp);
smart.Start();
IWorkItemResult result = smart.QueueWorkItem(delegate(object state)
{
return DateTime.Now;
}, new State());
SmartThreadPool.WaitAll(new IWorkItemResult[] { result });
Console.WriteLine(result.Result);
smart.Shutdown();
}
}
{
public void Dispose()
{
Console.WriteLine("Dispose...");
}
}
public class Program
{
static void Main(string[] args)
{
STPStartInfo stp = new STPStartInfo();
stp.DisposeOfStateObjects = true;
SmartThreadPool smart = new SmartThreadPool(stp);
smart.Start();
IWorkItemResult result = smart.QueueWorkItem(delegate(object state)
{
return DateTime.Now;
}, new State());
SmartThreadPool.WaitAll(new IWorkItemResult[] { result });
Console.WriteLine(result.Result);
smart.Shutdown();
}
}
输出:
Dispose...
2007-8-9 12:43:21
4. PostExecute CallBack
该委托在 WorkItem 结束时被调用。
SmartThreadPool smart = new SmartThreadPool();
smart.Start();
IWorkItemResult result = smart.QueueWorkItem(delegate(object state)
{
return DateTime.Now;
}, null, delegate(IWorkItemResult _result)
{
Console.WriteLine(_result.Result);
});
smart.Start();
IWorkItemResult result = smart.QueueWorkItem(delegate(object state)
{
return DateTime.Now;
}, null, delegate(IWorkItemResult _result)
{
Console.WriteLine(_result.Result);
});
5. Work Items Group
根据使用目的不同,我们可以将线程池划分为多个组。针对组进行管理,显然要比创建多个线程池对象要好些。
SmartThreadPool smart = new SmartThreadPool();
smart.Start();
IWorkItemsGroup group = smart.CreateWorkItemsGroup(3);
IWorkItemResult result = group.QueueWorkItem(delegate(object state)
{
return DateTime.Now;
}, null, delegate(IWorkItemResult _result)
{
Console.WriteLine(_result.Result);
});
group.WaitForIdle();
smart.Shutdown();
smart.Start();
IWorkItemsGroup group = smart.CreateWorkItemsGroup(3);
IWorkItemResult result = group.QueueWorkItem(delegate(object state)
{
return DateTime.Now;
}, null, delegate(IWorkItemResult _result)
{
Console.WriteLine(_result.Result);
});
group.WaitForIdle();
smart.Shutdown();