每次创建一个线程,都会花费几百微秒级别的时间来创建一个私有的局部栈,每个线程默认使用1M的内存。这个可以在使用Thread类的构造函数时设置:
- new Thread(new ThreadStart(Go), 2);
- new Thread(new ParameterizedThreadStart(Go("hello")), 3);
提供的两种构造函数方式都提供了对应的设置线程局部栈的大小。线程池通过共享和回收线程的方式来分配这些内存,这样可以使多线程运行在一个非常细粒度级别上而不影响性能。这对于充分利用多核处理器,使用分而治之的方式进行密集型计算的程序中很有用。同时线程池维护一个所有同时运行的工作线程总数的上限,如果有过多的活动线程就会加重操作系统的负担,使诸如CPU缓存失效等问题,当达到这个上限后,就要进行排队。这个线程队列使得任意并发的应用成为可能,如Web服务器就是这种原理。
有多种方式进入线程池:
- 通过Task Parallel Library(.NET 4 TPL)
- 通过调用ThreadPool.QueueUserWorkItem
- 通过异步委托方式
- 使用BackgroundWorker
- WCF、Remoting、ASP.NET、ASMX webservice
- System.Timers.Timer和System.Threading.Timer
- .NET中以Async结束命名的方法
- PLINQ
- 不能对放入线程池中的线程命名,这会使得调试更加困难
- 线程池中的线程总是后台线程
- 阻塞线程池中的线程困难引发额外的延迟
- CurrentTread.IsThreadPoolThread
- static void Main(string[] args)
- {
- Task.Factory.StartNew(Go);
- Console.WriteLine("Is Thread Pool: " + Thread.CurrentThread.IsThreadPoolThread.ToString());
- Console.ReadKey();
- }
- static void Go()
- {
- Console.Write("Is Thread Pool: " + Thread.CurrentThread.IsThreadPoolThread.ToString());
- }
Task.Factory.StartNew返回一个Task对象可以用来监控这个任务。
- static void Main(string[] args)
- {
- Task<string> task = Task.Factory.StartNew<string>(Go);
- Console.WriteLine("Is Thread Pool: " + Thread.CurrentThread.IsThreadPoolThread.ToString());
- if (task.IsCompleted)
- {
- string result = task.Result;
- Console.WriteLine(task.IsCompleted.ToString() + result);
- }
- Console.ReadKey();
- }
- static string Go()
- {
- return Thread.CurrentThread.IsThreadPoolThread.ToString();
- }
如果在工作线程中出现异常,当获取Task的Result属性时会重新引发AggregateException异常,如果没有查询Result或者没有调用Wait方法,所有未处理的异常都会终止进程执行。
- static void Main()
- {
- ThreadPool.QueueUserWorkItem (Go);
- ThreadPool.QueueUserWorkItem (Go, 123);
- Console.ReadLine();
- }
- static void Go (object data) // data will be null with the first call.
- {
- Console.WriteLine ("Hello from the thread pool! " + data);
- }
- static void Main()
- {
- Func<string, int> method = Work;
- method.BeginInvoke ("test", Done, method);
- // ...
- //
- }
- static int Work (string s) { return s.Length; }
- static void Done (IAsyncResult cookie)
- {
- var target = (Func<string, int>) cookie.AsyncState;
- int result = target.EndInvoke (cookie);
- Console.WriteLine ("String length is: " + result);
- }
三、线程池的优化
- 32位的.NET4.0环境为1023个
- 64位的.NET4.0环境为32768个
- .NET 3.5为每个CPU核 250个
- .NET2.0 为每个CPU核25个
- ThreadPool.SetMinThreads(50,50);