线程池的行为:没有最小尺寸成长 [英] ThreadPool behaviour: not growing from minimum size

查看:155
本文介绍了线程池的行为:没有最小尺寸成长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了我的线程池是这样的:

I had set up my thread pool like this:

ThreadPool.SetMaxThreads(10000, 10000);
ThreadPool.SetMinThreads(20, 20);



不过,我的应用程序开始重负载下挂。这似乎是因为工人的任务是不执行:我已经使用 ThreadPool.QueueUserWorkItem 运行一些任务,这反过来又用同样的方法来排队的进一步工作。这是一个有限的线程池(死锁情况)显然是危险的,但我使用一个线程池不限制最大线程数,但减少线程创建开销。

However, my app started hanging under heavy load. This seemed to be because worker tasks were not executing: I had used ThreadPool.QueueUserWorkItem to run some tasks which in turn used the same method to queue further work. This is obviously dangerous with a limited thread pool (a deadlock situation), but I am using a thread pool not to limit maximum threads but to reduce thread creation overhead.

我可以看到有潜在的陷阱,但我相信,在泳池设置一个最大的10000线程意味着如果一个项目排队,所有线程都很忙,在游泳池有没有10000线程,一个新的会,创建和任务处理的有

I can see the potential trap there, but I believed that setting a maximum of 10000 threads on the pool would mean that if an item was queued, all threads were busy, and there weren't 10000 threads in the pool, a new one would be created and the task processed there.

不过,我改成这样:

ThreadPool.SetMaxThreads(10000, 10000);
ThreadPool.SetMinThreads(200, 200);

..和应用程序开始工作。如果使其开始工作,我失去了一些关于如何/当线程池中最小朝最大容量扩展?

..and the app started working. If that made it start working, am I missing something about how/when the thread pool expands from minimum toward maximum size?

推荐答案

每当你使用线程池,你在它的线程注入和退休算法的摆布。

Whenever you use the thread pool, you are at the mercy of its "thread injection and retirement algorithm".

该算法不正确记录(据我所知)和不可配置。

The algorithm is not properly documented ( that I know of ) and not configurable.

如果你使用的任务,您可以编写自己的任务计划程序

If you're using Tasks, you can write your own Task Scheduler

这篇关于线程池的行为:没有最小尺寸成长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆