c# ThreadPool的问题
class Program
{
static void Main(string[] args)
{
int nWorkThreadCount, nIOThreadCount;
ThreadPool.GetMaxThreads(out nWorkThreadCount, out nIOThreadCount);
System.Console.WriteLine("WorkThreadCount is {0},IOThreadCount {1}", nWorkThreadCount, nIOThreadCount);
for (int i = 0; i < 10; ++i)
{
ThreadPool.QueueUserWorkItem(DoSomethind);
}
Console.ReadLine();
}
static int count = 0;
static void DoSomethind(object state)
{
//Console.WriteLine("Current Thread Name is {0},Current Count is {1}", Thread.CurrentThread.Name, count++);
Console.WriteLine("Current Thread ManagedThreadId is {0},Current Count is {1}", Thread.CurrentThread.ManagedThreadId, count++);
Thread.Sleep(10);
}
}
以上是对ThreadPool的测试,为什么在DoSomeThing里面使用我注释的代码,执行的效果并不是多线程的。
------解决方案--------------------你想看到线程冲突? 你可以循环9999次
------解决方案--------------------还有把 Thread.Sleep(10); 拿掉 会更明显