ThreadPoolExecutor参数说明

来源:互联网 发布:缅甸听音乐的软件 编辑:程序博客网 时间:2024/05/01 17:36

1、前言

目前很多框架都会需要有一个线程池,来维护所有的异步任务,管理的生命周期,也能够更方便的调用执行。所以ThreadPoolExecutor使用的时候,对其参数一定需要很清晰。所以自己去亲自动手,才是最放心的。

2、ThreadPoolExecutor构造方法源码

   /**     * Creates a new {@code ThreadPoolExecutor} with the given initial     * parameters.     *     * @param corePoolSize the number of threads to keep in the pool, even     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set     * @param maximumPoolSize the maximum number of threads to allow in the     *        pool     * @param 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.     * @param unit the time unit for the {@code keepAliveTime} argument     * @param workQueue the queue to use for holding tasks before they are     *        executed.  This queue will hold only the {@code Runnable}     *        tasks submitted by the {@code execute} method.     * @param threadFactory the factory to use when the executor     *        creates a new thread     * @param handler the handler to use when execution is blocked     *        because the thread bounds and queue capacities are reached     * @throws IllegalArgumentException if one of the following holds:<br>     *         {@code corePoolSize < 0}<br>     *         {@code keepAliveTime < 0}<br>     *         {@code maximumPoolSize <= 0}<br>     *         {@code maximumPoolSize < corePoolSize}     * @throws NullPointerException if {@code workQueue}     *         or {@code threadFactory} or {@code handler} is null     */    public ThreadPoolExecutor(int corePoolSize,                              int maximumPoolSize,                              long keepAliveTime,                              TimeUnit unit,                              BlockingQueue<Runnable> workQueue,                              ThreadFactory threadFactory,                              RejectedExecutionHandler handler) {        if (corePoolSize < 0 ||            maximumPoolSize <= 0 ||            maximumPoolSize < corePoolSize ||            keepAliveTime < 0)            throw new IllegalArgumentException();        if (workQueue == null || threadFactory == null || handler == null)            throw new NullPointerException();        this.corePoolSize = corePoolSize;        this.maximumPoolSize = maximumPoolSize;        this.workQueue = workQueue;        this.keepAliveTime = unit.toNanos(keepAliveTime);        this.threadFactory = threadFactory;        this.handler = handler;    }

3、参数说明

corePoolSize

 corePoolSize the number of threads to keep in the pool, even if they are idle, unless {@code allowCoreThreadTimeOut} is set

核心线程池的大小,表示线程池初始化的时候给核心存活的大小,就算没有任务需要执行,如果没有设置allowCoreThreadTimeOut的话,就会一直存活。

maximumPoolSize

maximumPoolSize the maximum number of threads to allow in the pool

线程池中最大可容纳的线程数,如果线程池中,提交的线程超过了corePoolSize,还可以继续提交,但是不能超过

keepAliveTime

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.

当线程池中的线程数大于核心线程数量时,无用的线程等待任务执行的最大存活的时间。

TimeUnit

unit the time unit for the {@code keepAliveTime} argument

时间单位的枚举,标识keepAliveTime的时间单位

workQueue

workQueue the queue to use for holding tasks before they are executed.  This queue will hold only the {@code Runnable} tasks submitted by the {@code execute} method.

管理通过execute方法提交的未执行的任务的队列

threadFactory

the factory to use when the executor creates a new thread

thread工厂类,用于executor产生新thread

handler

the handler to use when execution is blocked because the thread bounds and queue capacities are reached

当线程池达到最大值,且排队的队列满了的时候,就会拒绝新的任务加入时一个处理拒绝加入的回调。

0 0
原创粉丝点击