1、ThreadPoolExecutor的构造方法源码
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;
}
2、参数说明
核心线程数:corePoolSize
线程池中活跃的线程数,即使它们是空闲的,除非设置了allowCoreThreadTimeOut为true。allowCoreThreadTimeOut的值是控制核心线程数是否在没有任务时是否停止活跃的线程,当它的值为true时,在线程池没有任务时,所有的工作线程都会停止。
最大线程数:maximumPoolSize
线程池所允许存在的最大线程数。
多余线程存活时间:keepAliveTime(多余线程数 = 最大线程数 - 核心线程数)
线程池中除核心线程数之外的线程(多余线程)的最大存活时间,如果在这个时间范围内,多余线程没有任务需要执行,则多余线程就会停止。
时间单位:unit
多余线程存活时间的单位,可以是分钟、秒、毫秒等。
任务队列:workQueue
线程池的任务队列,当线程池无空闲线程来处理新提交的任务时,任务会存在这个队列里,当这个队列满了,线程池就会执行拒绝策略。
线程工厂:threadFactory
创建线程池的工厂,线程池将使用这个工厂来创建线程池,自定义线程工厂需要实现ThreadFactory接口。
拒绝执行处理器(也称拒绝策略):handler
当线程池无空闲线程,并且任务队列已满,此时将线程池将使用这个处理器来处理新提交的任务。


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



