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

问题描述

1)如果有空闲线程,为什么需要创建一个新线程来处理请求?

1) Why there is a need to create a new thread to handle the request if there are idle threads?

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 - 核心和最大池大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:21
查看更多