什么时候在ThreadPoolExecutor中指定单独的核心和最大池大小一个好主意? [英] When is specifying separate core and maximum pool sizes in ThreadPoolExecutor a good idea?

查看:220
本文介绍了什么时候在ThreadPoolExecutor中指定单独的核心和最大池大小一个好主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to understand the point in specifying separate core and maximum pool sizes for Java 5's ThreadPoolExecutor. My understanding is that the number of threads is only increased once the queue is full, which seems a bit late (at least with larger queues).

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

Isn't it that I'm either happy to allocate a larger number of threads to the tasks, in which case I might just increase the core pool size; or I am not really willing to do so, in which case I should rather have a larger queue? What is a scenario where separate core and maximum pool sizes are useful?

推荐答案

http://comments.gmane.org/gmane.comp.java.jsr.166-concurrency/7109rel =nofollow>此处。

There is a discussion of this here.


池将在正常负载下工作在corePoolSize(到
,它会上升,除非使用预启动)。当过载
条件发生时(由于有比工作者更多的待处理/正在处理的
任务定义),我们使用队列作为缓冲区 - 用
期望正常工作量将在接近
的未来恢复。如果我们担心过载,那么我们可以使用一个
有界队列,并说如果队列填满了添加更多的工人
到maxPoolSize。如果我们使用无界队列,我们​​说我们不要
期望(或者不关心)过载。

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.

目的是平衡处理预期工作负载的能力,即使在暂时超载情况下,
,而没有过多的线程
创建和没有太多线程churn(即
create-work-die-create)。

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).

这篇关于什么时候在ThreadPoolExecutor中指定单独的核心和最大池大小一个好主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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