TPE-ThreadPoolExecutor
程序员文章站
2024-02-20 10:50:52
...
TPE:
java.util.concurrent.ThreadPoolExecutor
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
@NotNull TimeUnit unit,
@NotNull BlockingQueue<Runnable> workQueue,
@NotNull ThreadFactory threadFactory,
@NotNull RejectedExecutionHandler handler)
Creates a new ThreadPoolExecutor with the given initial parameters.
Parameters:
corePoolSize - the number of threads to keep in the pool, even if they are idle, unless allowCoreThreadTimeOut is set
在池中保持的线程数,即使它们是空闲的,除非设置了allowCoreThreadTimeOut
maximumPoolSize - the maximum number of threads to allow in the pool
在池中允许的最大线程数
keepAliveTime - when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.
当线程数大于核心数时,这是多余空闲线程在终止前等待新任务的最大时间。
unit - the time unit for the keepAliveTime argument
时间单位的keepAliveTime参数
workQueue - the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.
队列用于在执行任务之前执行任务。此队列只保留执行方法提交的可运行任务。
threadFactory - the factory to use when the executor creates a new thread
在执行器创建新线程时使用的工厂
handler - the handler to use when execution is blocked because the thread bounds and queue capacities are reached
在执行被阻塞时使用的handler,因为到达了线程边界和队列容量
Throws:
IllegalArgumentException - if one of the following holds:
corePoolSize < 0
keepAliveTime < 0
maximumPoolSize <= 0
maximumPoolSize < corePoolSize
NullPointerException - if workQueue or threadFactory or handler is null
allowCoreThreadTimeOut -- If false (default), core threads stay alive even when idle. If true, core threads use keepAliveTime to time out waiting for work
如果false(默认),核心线程即使在空闲时仍然存活。如果是true,核心线程使用keepAliveTime来等待工作
也就是说,如果allowCoreThreadTimeOut不改变(即:false), 则corePoolSize计数中的Thread即便是idle空闲的,也保持alive状态;
如果被设置成true了,此时keepAliveTime就派上用场了,
JDK:
ThreadPoolExecutor
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
用给定的初始参数创建新的 ThreadPoolExecutor。
参数:
corePoolSize - 池中所保存的线程数,包括空闲线程。
maximumPoolSize - 池中允许的最大线程数。
keepAliveTime - 当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间。
unit - keepAliveTime 参数的时间单位。
workQueue - 执行前用于保持任务的队列。此队列仅保持由 execute 方法提交的 Runnable 任务。
threadFactory - 执行程序创建新线程时使用的工厂。
handler - 由于超出线程范围和队列容量而使执行被阻塞时所使用的处理程序。
抛出:
IllegalArgumentException - 如果 corePoolSize 或 keepAliveTime 小于 0,或者 maximumPoolSize 小于等于 0,或者 corePoolSize 大于 maximumPoolSize。
NullPointerException - 如果 workQueue、 threadFactory 或 handler 为 null。