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;
}
线程池理解为银行的网点
- int corePoolSize:常驻核心线程数,创建完线程以后,当有请求任务时,就会安排核心线程去完成任务,即理解为今日银行的办理窗口,当到达corePoolSize数量以后,就会放到缓存队列中。
- maximumPoolSize:当任务到达corePoolSize数量以后,缓存队列中也满了,就会开启最大线程数,即理解为非今日银行的办理窗口也开始工作。
- long keepAliveTime:多余空闲线程数的存活时间,当前线程数大于corePoolSize,并且等待时间大于
keepAliveTime,多于线程或被销毁直到剩下corePoolSize为止。 - TimeUnit unit: keepAliveTime的单位。
- BlockingQueue workQueue:阻塞队列,被提交但未必执行的任务,即银行网点的候客区。
- ThreadFactory threadFactory:用于创建线程池中工作线程的线程工厂,一般用默认的。
- RejectedExecutionHandler、 handler:拒绝策略,当堵塞队列满了并且工作线程大于线程池的最大线程数(maximumPoolSize)。