using System; using System.Threading; namespace ConsoleApplication1 { static class Program { static int _count; private const int Max = 1000; static void Main() { ThreadPool.SetMaxThreads(3, 3); for (int i = 0; i < Max; i++) { ThreadPool.QueueUserWorkItem(Oper, i); } while (_count < Max) Thread.Sleep(50); Console.WriteLine("{0}\tcount={1}", DateTime.Now.ToString("mm:ss:fff"), _count); int workerthread = -1; int streamthread = -1; ThreadPool.GetAvailableThreads(out workerthread, out streamthread); Console.WriteLine("{0}\tworkerthread={1}\tstreamthread={2}", DateTime.Now.ToString("mm:ss:fff"), workerthread, streamthread); Console.ReadLine(); } static void Oper(object obj) { int i = (int)obj; Console.WriteLine("{0}\t执行结果{1}", DateTime.Now.ToString("mm:ss:fff"), i); _count++; } } }
简单解释一下,开3个线程,执行1000个任务。
最终的空闲线程还是3个。
浙公网安备 33010602011771号