您的位置:首页 > 其它

简单线程系列1-最简单的线程池

2012-06-03 09:46 197 查看
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个。

程序下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: