ThreadPoolExecutor参数说明

本文详细解析了线程池创建时的各项参数配置:核心线程数、最大线程数、空闲线程存活时间等,并介绍了阻塞队列、线程工厂及拒绝策略的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    /**
     * 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)


int corePoolSize线程池核心线程数,初始线程数为0,在不超过此值的情况下,每次提交一个任务,就新起一个线程。
int maximumPoolSize线程池最大线程数,核心线程都在运行,又有新任务提交进来时且当前线程池线程数不超过此值时,新起线程执行任务。
long keepAliveTime当前线程池数目大于核心线程数,且有线程处于idle状态时,当线程处于idle的时间超过keepAliveTime和TimeUnit指定的时间时,线程就会被回收
BlockingQueue<Runnable> workQueue阻塞队列,当线程池中线程数处于最大且都处于执行状态时,再提交进来任务放在此队列中
ThreadFactory threadFactor提供线程池新起线程的方法
RejectedExecutionHandler handler)当线程池满且阻塞队列也满的情况下,提交进来的任务,由此对象处理,需要自定义处理策略









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值