ThreadPoolExecutor 线程池的七大参数

本文详细解析了线程池构造函数中的七个关键参数:corePoolSize、maximumPoolSize、keepAliveTime、unit、workQueue、threadFactory 和 handler 的含义及作用。

源码

 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.acc = System.getSecurityManager() == null ?
                null :
                AccessController.getContext();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }

一、corePoolSize

线程池中的常驻核心线程数

二、maximumPoolSize

线程池中能够容纳同时执行的最大线程数,此值必须大于等于1

三、keepAliveTime

多余的空闲线程的存活时间,当前线程池中超过corePoolSize时,当空闲时间达到keepAliveTime时,多余线程会被销毁直到只剩下corePoolSize个线程为止

四、unit

keepAliveTime的单位

五、workQueue

任务队列,任务被提交但是尚未被执行的任务,将会形成队列等待

六、threadFactory

表示生成线程池中工作线程的线程工厂,用于创建线程,一般默认即可

七、handle

拒绝策略,表示队列满了,并且工作线程大于等于线程池的最大线程数的最大线程数(maximumPoolSize)时 如何来拒绝请求执行的策略

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值