java – 什么时候在ThreadPoolExecutor中指定单独的核心和最大池大小是一个好主意?

我试图理解为Java 5的ThreadPoolExecutor指定单独的核心和最大池大小的重点.我的理解是,一旦队列满了,线程数就会增加,这似乎有点晚(至少有较大的队列).

是不是我很高兴为任务分配更多的线程,在这种情况下,我可能只是增加核心池大小;或者我不是真的愿意这样做,在这种情况下我宁愿有更大的队列?什么是单独的核心和最大池大小有用的场景?

解决方法:

讨论了这个here.

The pool is intended to work under normal load at corePoolSize (to
which it ramps up unless pre-start is used). When an overload
condition occurs (defined by there being more pending/in-process
tasks than workers) we use the queue to act as a buffer – with the
expectation that the normal workload will be restored in the near
future. If we’re worried about excessive overload then we can use a
bounded queue, and that says “if the queue fills up add more workers
up to maxPoolSize”. If we use an unbounded queue we saying we don’t
expect (or else don’t care about) excessive overload.

The aim is to balance the ability to handle the expected workload,
even under transient overloads, without having excessive thread
creation and without too much thread churn (ie
create-work-die-create).

上一篇:c# – 启动异步线程的当前和推荐方法是什么?


下一篇:java – 使用线程池和优先级队列的调度程序?