ThreadPoolExecutor - 核心和最大池大小 [英] ThreadPoolExecutor - Core and maximum pool sizes

查看:108
本文介绍了ThreadPoolExecutor - 核心和最大池大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


在方法 execute(java.lang.Runnable)中提交新任务时,少于 corePoolSize 线程正在运行,即使其他工作线程处于空闲状态,也会创建一个新线程来处理请求。

When a new task is submitted in method execute(java.lang.Runnable),and fewer than corePoolSize 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 than maximumPoolSize 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:


  1. 如果线程数小于corePoolSize,则创建一个新线程以运行新任务。

  2. 如果线程数相等(或更大)比)corePoolSize,将任务放入队列。

  3. 如果队列已满,并且线程数小于maxPoolSize,则创建一个新线程来运行任务。

  4. 如果队列已满,并且线程数大于或等于maxPoolSize,则拒绝该任务。

  1. If the number of threads is less than the corePoolSize, create a new Thread to run a new task.
  2. If the number of threads is equal (or greater than) the corePoolSize, put the task into the queue.
  3. If the queue is full, and the number of threads is less than the maxPoolSize, create a new thread to run tasks in.
  4. If the queue is full, and the number of threads is greater than or equal to maxPoolSize, reject the task.

全文

这篇关于ThreadPoolExecutor - 核心和最大池大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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