代码示例:
class="code_img_closed" src="/Upload/Images/2017072305/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('91f81194-2ed1-49cb-af12-c6eb91db230b',event)" src="/Upload/Images/2017072305/2B1B950FA3DF188F.gif" alt="" />
class ThreadPoolDemo { public static void start() { int workerThreads, completionPortThreads; //设置线程最大值 ThreadPool.SetMaxThreads(5, 5); ////设置线程最小值 ThreadPool.SetMinThreads(2, 2); //检索线程池在新请求预测中维护的空闲线程数 ThreadPool.GetMinThreads(out workerThreads, out completionPortThreads); Console.WriteLine("workerThreads:" + workerThreads); Console.WriteLine("completionPortThreads:" + workerThreads); //绑定线程,当有可用线程时执行。 ThreadPool.QueueUserWorkItem(WaitCallback, "第1个"); ThreadPool.QueueUserWorkItem(WaitCallback, "第2个"); ThreadPool.QueueUserWorkItem(WaitCallback, "第3个"); ThreadPool.QueueUserWorkItem(WaitCallback, "第4个"); ThreadPool.QueueUserWorkItem(WaitCallback, "第5个"); ThreadPool.QueueUserWorkItem(WaitCallback, "第6个"); ThreadPool.QueueUserWorkItem(WaitCallback, "第7个"); ThreadPool.QueueUserWorkItem(WaitCallback, "第8个"); ThreadPool.QueueUserWorkItem(WaitCallback, "第9个"); ThreadPool.QueueUserWorkItem(WaitCallback, "第10个"); } static void WaitCallback(object state) { Console.WriteLine("线程调用" + state); int workerThreads, completionPortThreads; //检测可用线程和io线程数 ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads); Console.WriteLine("workerThreads:" + workerThreads); Console.WriteLine("completionPortThreads:" + workerThreads); Thread.Sleep(2000); if ("第2个" == state.ToString()) { Thread.Sleep(6000); } Console.WriteLine(state + "调用结束"); } }View Code