ThreadPoolExecutor - 核心和最大池大小 [英] ThreadPoolExecutor - Core and maximum pool sizes
问题描述
在方法
execute(java.lang.Runnable)
中提交新任务时,少于corePoolSize
线程正在运行,即使其他工作线程处于空闲状态,也会创建一个新线程来处理请求。
When a new task is submitted in method
execute(java.lang.Runnable)
,and fewer thancorePoolSize
threads are running, a new thread is created to handle the request, even if other worker threads are idle.
1)如果有空闲线程,为什么需要创建一个新线程来处理请求?
1) Why there is a need to create a new thread to handle the request if there are idle threads?
如果有超过
corePoolSize
但小于maximumPoolSize
线程正在运行,只有队列已满时才会创建新线程。
If there are more than
corePoolSize
but less thanmaximumPoolSize
threads running, a new thread will be created only if the queue is full.
2)我不明白 corePoolSize
和<$之间的区别c $ c> maximumPoolSize 此处。其次,当线程小于 maximumPoolSize
时,队列如何填满?如果线程等于或大于 maximumPoolSize
,则队列只能是满的。不是吗?
2) I don't understand the difference between corePoolSize
and maximumPoolSize
here. Secondly, how can a queue be full when threads are less than maximumPoolSize
? Queue can only be full if threads are equal to or more than maximumPoolSize
. Isn't it?
推荐答案
以下是Sun的简单术语创建规则:
Here are Sun’s rules for thread creation in simple terms:
- 如果线程数小于corePoolSize,则创建一个新线程以运行新任务。
- 如果线程数相等(或更大)比)corePoolSize,将任务放入队列。
- 如果队列已满,并且线程数小于maxPoolSize,则创建一个新线程来运行任务。
- 如果队列已满,并且线程数大于或等于maxPoolSize,则拒绝该任务。
- If the number of threads is less than the corePoolSize, create a new Thread to run a new task.
- If the number of threads is equal (or greater than) the corePoolSize, put the task into the queue.
- If the queue is full, and the number of threads is less than the maxPoolSize, create a new thread to run tasks in.
- If the queue is full, and the number of threads is greater than or equal to maxPoolSize, reject the task.
这篇关于ThreadPoolExecutor - 核心和最大池大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!