java线程池ThreadPoolExecutor类核心方法解析
完整构造线程池
/**
* 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
*/
public ThreadPoolExecutor(int corePoolSize,//核心线程数(核心线程不会被销毁)
int maximumPoolSize,//最大线程数
long keepAliveTime,//超过核心线程数的线程的最大空闲生存时间,其后将可能被销毁
TimeUnit unit,//keepAliveTime的单位
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;
}
ThreadPoolExecutor::submit
提交一个允许有返回值的任务,Future::get获取返回值.
public Future<?> submit(Runnable task) {
if (task == null) throw new NullPointerException();
//RunnableFuture自己就是一个Runnable且同时是一个Future可以用来接收返回值
RunnableFuture<Void> ftask = newTaskFor(task, null);
//执行execute,添加woker运行指定task
execute(ftask);
return ftask;
}
ThreadPoolExecutor::execute
- 如果核心线程没满开核心线程,否则将任务加入任务队列.
- 如果此时线程池关了,出队任务并执行拒绝策略.
- 如果核心线程设定为0且工作线程为0,则开非核心线程并执行队列中的任务.
- 如果队列满了开非核心线程,如果失败了执行拒绝策略.
- 如果核心线程数设定大于0,只要任务队列没满就最多只会有核心线程.非核心线程会在指定时间后销毁.
- 如果核心线程数设定等于0,在任务队列第一次满之前就最多只有一个非核心线程.
/**
* Executes the given task sometime in the future. The task
* may execute in a new thread or in an existing pooled thread.
*
* If the task cannot be submitted for execution, either because this
* executor has been shutdown or because its capacity has been reached,
* the task is handled by the current {@link RejectedExecutionHandler}.
*
* @param command the task to execute
* @throws RejectedExecutionException at discretion of
* {@code RejectedExecutionHandler}, if the task
* cannot be accepted for execution
* @throws NullPointerException if {@code command} is null
*/
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
/*
* Proceed in 3 steps:
*
* 1. If fewer than corePoolSize threads are running, try to
* start a new thread with the given command as its first
* task. The call to addWorker atomically checks runState and
* workerCount, and so prevents false alarms that would add
* threads when it shouldn't, by returning false.
*
* 2. If a task can be successfully queued, then we still need
* to double-check whether we should have added a thread
* (because existing ones died since last checking) or that
* the pool shut down since entry into this method. So we
* recheck state and if necessary roll back the enqueuing if
* stopped, or start a new thread if there are none.
*
* 3. If we cannot queue task, then we try to add a new
* thread. If it fails, we know we are shut down or saturated
* and so reject the task.
*/
/*
* 分三步进行:
* 1. 如果运行的线程少于corePoolSize,
* 尝试以command作为第一个task开启一个一个新核心线程.
* 2. 如果成功将command入队workQueue,
* 双重检测确保线程池正RUNNING,
* (可能有其他线程执行了shutdown).
* 如果线程池已经shutdown,则回滚入队操作,
* 并执行拒绝策略
* 3. 如果无法入队,直接添加新的工作线程并执行command,
* 如果操作失败了,则说明线程池可能已经shutdown或饱和了,
* 则执行拒绝策略
*/
//获取ctl快照
int c = ctl.get();
//第一步
//判断工作线程数是否少于设定的核心线程数值
if (workerCountOf(c) < corePoolSize) {
//添加核心工作线程
if (addWorker(command, true))
return;
//重新获取ctl快照(ctl可能已被其他线程修改)
c = ctl.get();
}
//第二部
//如果线程池正RUNNING,将command加入workQueue
if (isRunning(c) && workQueue.offer(command)) {
//重新获取ctl快照
int recheck = ctl.get();
//双重检测,确保线程池没有shutdown,如果shutdown了则将command出队workQueue
if (! isRunning(recheck) && remove(command))
//执行拒绝策略
reject(command);
//判断此时线程池正RUNNING,且工作线程为0(corePoolSize可被设定为0)
else if (workerCountOf(recheck) == 0)
//添加非核心线程,并从workQueue中取出首个command运行
addWorker(null, false);
}
//队列可能已满从而失败的情况下,直接添加非核心工作线程,并将command作为task运行
else if (!addWorker(command, false))
//执行addWorker失败(线程池关闭或饱和)则执行拒绝策略
reject(command);
}
ThreadPoolExecutor::addWorker
/*
* Methods for creating, running and cleaning up after workers
*/
/**
* Checks if a new worker can be added with respect to current
* pool state and the given bound (either core or maximum). If so,
* the worker count is adjusted accordingly, and, if possible, a
* new worker is created and started, running firstTask as its
* first task. This method returns false if the pool is stopped or
* eligible to shut down. It also returns false if the thread
* factory fails to create a thread when asked. If the thread
* creation fails, either due to the thread factory returning
* null, or due to an exception (typically OutOfMemoryError in
* Thread.start()), we roll back cleanly.
*
* @param firstTask the task the new thread should run first (or
* null if none). Workers are created with an initial first task
* (in method execute()) to bypass queuing when there are fewer
* than corePoolSize threads (in which case we always start one),
* or when the queue is full (in which case we must bypass queue).
* Initially idle threads are usually created via
* prestartCoreThread or to replace other dying workers.
*
* @param core if true use corePoolSize as bound, else
* maximumPoolSize. (A boolean indicator is used here rather than a
* value to ensure reads of fresh values after checking other pool
* state).
* @return true if successful
*/
private boolean addWorker(Runnable firstTask, boolean core) {
retry:
for (int c = ctl.get();;) {//死循环,每次循环获取ctl最新快照
// Check if queue empty only if necessary.
// 必要时检测workQueue是否为空.(这里利用与或行为的短路一层一层判断)
// 什么是必要条件:当且仅当线程池被SHUTDOWN的时候,且不再有新任务.
// 即:addWorker时,如果线程池已经SHUTDOWN就不再接受新任务,但继续消费workQueue中的任务.
if (
//1.检测线程是否已经被SHUTDOWN,如果此时还是RUNNING就直接执内循环,否则如果至少是SHUTDOWN则进入下个与(进入下一个与线程池至少SHUTDOWN,甚至是STOP)
runStateAtLeast(c, SHUTDOWN)
&& (
//2.1.检查线程是否已经被STOP,如果被STOP了就不再消费workQueue,返回false,如果小于STOP则进入下一个或(进入下一个或线程池必然处在SHUTDOWN)
runStateAtLeast(c, STOP)
//2.2.如果有指定要执行的任务,由于此时线程池已经SHUTDOWN,不接收新任务,直接返回false,如果没给定新任务则进入下一个或
|| firstTask != null
//2.3. 如果任务队列为空,此时线程池也正处在SHUTDOWN,同时也没有新任务,则返回false,否则需要进入内循环消费workQueue剩余任务
|| workQueue.isEmpty()
)
)
//执行失败(三种情况:1.线程池已经STOP,2.线城池是SHUTDOWN但指定了新任务,3.线城池是SHUTDOWN且workQueue为空)
return false;
for (;;) {
//当前线程数:1.如果是add核心线程,判断是否大于等于核心线程数,否则判断是否大于等于最大线程数
if (workerCountOf(c)
>= ((core ? corePoolSize : maximumPoolSize) & COUNT_MASK))
//线程池饱和,执行失败
return false;
//上面判断都过了,说明此时可以添加任务,CAS先将线程数加一(如果后面实际添加worker执行失败再回退),CAS执行成功则跳出外循环,执行下面的添加worker
if (compareAndIncrementWorkerCount(c))
break retry;
//重新获取ctl快照,确保获取到的是最新的值(值传递)
c = ctl.get(); // Re-read ctl
//如果此时状态至少是SHUTDOWN,则重新执行外循环
if (runStateAtLeast(c, SHUTDOWN))
continue retry;
// else CAS failed due to workerCount change; retry inner loop
// 否则,重新执行内循环将线程数加一
}
}
boolean workerStarted = false;
boolean workerAdded = false;
Worker w = null;
try {
//新建worker,并将firstTask丢进入,以保证如果有firstTask的情况下它会最先执行
//内部线程的run方法会runWorker方法,runWorker会循环从workQueue取任务执行
w = new Worker(firstTask);
//拿到worker内部新建的线程快照
final Thread t = w.thread;
if (t != null) {
final ReentrantLock mainLock = this.mainLock;
//这里的操作需要加锁主要是因为workers是HashSet,线程不安全
mainLock.lock();
try {
// Recheck while holding lock.
// Back out on ThreadFactory failure or if
// shut down before lock acquired.
// 在获得锁后重新检测,以确保线程池正处在正常运行状态
// 重新获取最新快照
int c = ctl.get();
//如果正RUNNING,则直接添加worker到集合中
if (isRunning(c) ||
//否则如果线程池是SHUTDOWN且没有新任务的情况下才添加worker到集合中
(runStateLessThan(c, STOP) && firstTask == null)) {
//如果线程不是处与新建状态,抛出异常(因为后面会执行start)
if (t.getState() != Thread.State.NEW)
throw new IllegalThreadStateException();
//添加worker到集合中
workers.add(w);
//修改worker添加状态
workerAdded = true;
//修改总worker数量
int s = workers.size();
if (s > largestPoolSize)
largestPoolSize = s;
}
} finally {
//解锁
mainLock.unlock();
}
//如果已经添加了worker,说明此时worker创建成功,且内部的线程没有开始运行,则使其运行
if (workerAdded) {
t.start();
//修改worker启动状态
workerStarted = true;
}
}
} finally {
//如果worker线程被启动失败
if (! workerStarted)
//回退上面的工作线程数加一操作,并将worker从集合中移除(如果worker已经被加入了集合的话),并执行tryTerminate内部的terminated钩子
addWorkerFailed(w);
}
return workerStarted;
}
ThreadPoolExecutor::beforeExecute和afterExecute模板方法
重写这两个钩子以实现类似AOP的效果.
class MyThreadPoolExecutor extends ThreadPoolExecutor {
public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
}
@Override
//runWorker内部执行task.run()前执行这个钩子方法
protected void beforeExecute(Thread t, Runnable r) {
super.beforeExecute(t, r);
System.out.println("执行任务前的钩子,已执行"+this.getTaskCount());
}
@Override
//runWorker内部执行task.run()后执行这个钩子方法,如果run抛出了异常可以在此捕获处理
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
System.out.println("执行任务后的钩子,完成执行"+this.getTaskCount());
}
}