[英]Thread-pool don't use threads as expected?
I'm trying to see what happens to the thread-pool when I extremely use it's threads : 我试图查看当我极端使用线程池时线程池会发生什么:
So I run this test : 所以我运行了这个测试:
void Main()
{
int AvaliableWorker,AvaliableioCompletion,MaxWorker,MaxioCompletion;
for (int i=0;i<1028;i++) Task.Factory.StartNew(()=>{Thread.Sleep(5000);});
Thread.Sleep(2000); // give the loop some time to run threads....
ThreadPool.GetAvailableThreads(out AvaliableWorker, out AvaliableioCompletion);
Console.WriteLine("{0} / {1}", AvaliableWorker, AvaliableioCompletion);
ThreadPool.GetMaxThreads(out MaxWorker, out MaxioCompletion);
Console.WriteLine("{0} / {1}", MaxWorker, MaxioCompletion);
}
However , When I run it on my .net4/32bit
environment (console) , I get these results : 但是,当我在
.net4/32bit
环境(控制台)上运行它时,得到以下结果:
**1015** / 1000
1023 / 1000
Running again : 再次运行:
**1009** / 1000
1023 / 1000
The question is about the bold numbers : 问题是关于粗体数字:
Question 题
Fw.4
sets a default max thread-pool worker threads creation up to 1023
(as you can see). Fw.4
设置默认的最大线程池工作线程最大创建数为1023
(如您所见)。
But looking at GetAvailableThreads
: 但是看着
GetAvailableThreads
:
If so , how come I still get big values like 1015 , 1009
right as I make 1028 threads sleep ? 如果是这样, 当我使1028线程进入睡眠状态时 ,为什么我仍然会得到
1015 , 1009
这样的大值?
I mean , 1023-1028 = negative number
: ( in other words : "no more threads my friend , I will create them now on demand via 2 threads per second — but that's another unrelated topic ) 我的意思是,
1023-1028 = negative number
:(换句话说: “我的朋友不再有线程,我现在将通过每秒2个线程按需创建它们, 但这是另一个不相关的主题 )”
right as I make 1028 threads sleep ?
当我使1028个线程进入睡眠状态时对吗?
You didn't. 你没有 You queued about a 1000 Tasks, not (yet) Threads.
您排队约1000个任务,而不是(尚未)线程。 Still waiting to run when you WriteLine() the results.
当您WriteLine()结果时,仍在等待运行。
The actual number of pool threads at that moment: 1023 - 1015 = 8. 此时的实际池线程数:1023-1015 = 8。
Make the Sleep() after the for loop a little longer (seconds) and you will see a lower number of available threads, ie a higher number of pool threads. 使for循环后的Sleep()稍长一些(几秒钟),您将看到较少的可用线程,即较多的池线程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.