在jdk1.8的类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.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
/**
* 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
*/
但是有时候在面试的过程中,会问到你问到这些参数的意思。上面的注释就是对各个参数的解释,在接下来的过程中将分别解释线程池的7个参数。
/**
* 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
corePoolSize: 即使有些线程是闲置的,都要保持池中核心线程的数量,除非你设置了allowCoreThreadTimeOut参数
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
maximumPoolSize:参数maximumPoolSize是在池中允许的最大线程数
* @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.
keepAliveTime(存活时间) :当某个线程的线程数远大于核心数时,在新任务中大量的空闲线程一直处于等待状态,那就回收它。
* @param unit the time unit for the {@code keepAliveTime} argument
unit: keepAliveTime参数的时间单位
* @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.
workQueue(存放待执行任务的队列):在任务执行之前提交任务到队列中使用。这个队列仅仅存放Runnable任务被execute方法提交。(又可以叫做存放处于等待状态线程的队列)
* @param threadFactory the factory to use when the executor
* creates a new thread
threadFactory(线程工厂):创建线程时工厂开始使用。
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
handler(拒绝策略):当到达最大线程数或者是队列中都是任务时,提交的任务将会被阻塞,这时候就执行拒绝策略。
这篇文章主要讲线程池的7个参数,在下一篇文章中继续讲,当我们自定义一个线程池时,线程池的参数将如何设置。