源码
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)时 如何来拒绝请求执行的策略
本文详细解析了线程池构造函数中的七个关键参数:corePoolSize、maximumPoolSize、keepAliveTime、unit、workQueue、threadFactory 和 handler 的含义及作用。
4万+

被折叠的 条评论
为什么被折叠?



