1.常用变量解析
//ctl可以看作一个int类型的数字,高3位表示线程池的状态,低29位表示worker数量
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
//integer.size为32,所以COUNT_BITS为29,表示低29位
private static final int COUNT_BITS = Integer.SIZE - 3;
//线程允许的最大线程数,1左移29位再减1,即2^29-1
private static final int CAPACITY = (1 << COUNT_BITS) - 1;
// runState is stored in the high-order bits
//线程池5种状态,大小排序:RUNNING<SHUTDOWN<STOP<TIDYING<TERMINATED
private static final int RUNNING = -1 << COUNT_BITS;
private static final int SHUTDOWN = 0 << COUNT_BITS;
private static final int STOP = 1 << COUNT_BITS;
private static final int TIDYING = 2 << COUNT_BITS;
private static final int TERMINATED = 3 << COUNT_BITS;
// Packing and unpacking ctl
//获取线程池状态,通过按位与操作,低29位将全部变成0
private static int runStateOf(int c) { return c & ~CAPACITY; }
//获取线程池worker数量,通过位与操作,高3位将全部变成0
private static int workerCountOf(int c) { return c & CAPACITY; }
//根据线程池状态和线程池worker数量,生成ctl值
private static int ctlOf(int rs, int wc) { return rs | wc; }
/*
* Bit field accessors that don't require unpacking ctl.
* These depend on the bit layout and on workerCount being never negative.
*/
//线程池状态小于xx
private static boolean runStateLessThan(int c, int s) {
return c < s;
}
//线程池状态大于等于xx
private static boolean runStateAtLeast(int c, int s) {
return c >= s;
}
//线程池状态是否处于运行状态
private static boolean isRunning(int c) {
return c < SHUTDOWN;
}
2.构造方法
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;
//根据传入参数unit和keepalivetime,将存活时间转换为纳秒
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
3.提交执行task的过程
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.
*/
int c = ctl.get();
//workker数量小于核心线程数,则直接创建woker执行任务
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
//workker数量超过核心线程数,任务直接进入队列
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
//线程池如果不是running状态,说明执行过shutdown命令,需要对新入的任务执行reject操作
//这里recheck,是因为任务入队列前后,线程池的状态可能会发生变化
if (! isRunning(recheck) && remove(command))
reject(command);
//这里判断0值,主要是在线程池构造方法中,核心线程数允许为0
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
//如果线程池不是运行状态,或者任务进入队列失败,则尝试创建worker执行任务
//有3点需要注意:
//1.线程池不是运行状态时,addWorker内部会判断线程池状态
//2.addWorker第2个参数表示是否有创建核心线程
//3.addWorker返回false,则说明执行任务失败,需要执行reject操作
else if (!addWorker(command, false))
reject(command);
}
4.addWorker源码解析
private boolean addWorker(Runnable firstTask, boolean core) {
retry:
//外层自旋
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
// Check if queue empty only if necessary.
//这个条件比较复杂,对其进行调整便于理解,和下面条件等价
//(rs > SHUTDOWN) ||
//(rs == SHUTDOWN && firstTask != null) ||
//(rs == SHUTDOWN && workQueue.isEmpty())
//1.线程池状态大于SHUTDOWN时,直接返回false
//2.线程池状态等于SHUTDOWN时,且firstTask不为null,直接返回false
//3.线程池状态等于SHUTDOWN,且队列为空,直接返回false
if (rs >= SHUTDOWN &&
! (rs == SHUTDOWN &&
firstTask == null &&
! workQueue.isEmpty()))
return false;
//内层自旋
for (;;) {
int wc = workerCountOf(c);
//worker的数量超过容量,直接返回false
if (wc >= CAPACITY ||
wc >= (core ? corePoolSize : maximumPoolSize))
return false;
//使用CAS方式增加worker数量
//若增加成功,则直接跳出外层循环进入到第二部分
if (compareAndIncrementWorkerCount(c))
break retry;
c = ctl.get(); // Re-read ctl
//线程池状态发生变化,对外层进行自旋
if (runStateOf(c) != rs)
continue retry;
//其他情况,直接内层循环进行自旋即可
// else CAS failed due to workerCount change; retry inner loop
}
}
boolean workerStarted = false;
boolean workerAdded = false;
Worker w = null;
try {
w = new Worker(firstTask);
final Thread t = w.thread;
if (t != null) {
final ReentrantLock mainLock = this.mainLock;
//worker的添加必须是串行的,因此必须要加锁
mainLock.lock();
try {
// Recheck while holding lock.
// Back out on ThreadFactory failure or if
// shut down before lock acquired.
//重新检查线程池的状态
int rs = runStateOf(ctl.get());
if (rs < SHUTDOWN ||
(rs == SHUTDOWN && firstTask == null)) {
//worker已经调用过了start()方法,则不再创建worker
if (t.isAlive()) // precheck that t is startable
throw new IllegalThreadStateException();
//worker创建并添加到workers成功
workers.add(w);
//更新largestPoolSize变量
int s = workers.size();
if (s > largestPoolSize)
largestPoolSize = s;
workerAdded = true;
}
} finally {
mainLock.unlock();
}
//启动worker线程
if (workerAdded) {
t.start();
workerStarted = true;
}
}
} finally {
//worker线程启动失败,说明线程池状态发生了变化(关闭操作被执行),需进行shutdown相关操作
if (! workerStarted)
addWorkerFailed(w);
}
return workerStarted;
}
5.runWorker核心线程执行逻辑
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();
Runnable task = w.firstTask;
w.firstTask = null;
//调用unlock()是为了让外部可以中断
w.unlock(); // allow interrupts
boolean completedAbruptly = true;
try {
//这是自旋
//1.如果firstTask不为null,则执行firstTask
//2.如果firstTask为null,则调用getTask()从队列获取任务
//3.阻塞队列的特性就是:当队列为空时,当前线程会被阻塞等待
while (task != null || (task = getTask()) != null) {
//这对worker进行了加锁,目的是:
//1.降低锁范围,提升性能
//2.保证每个worker执行的任务是串行的
//worker本身继承了AbstractQueuedSynchronizer,本身就是一把锁,因此可以直接lock
w.lock();
// If pool is stopping, ensure thread is interrupted;
// if not, ensure thread is not interrupted. This
// requires a recheck in second case to deal with
// shutdownNow race while clearing interrupt
//如果线程池正在停止,则对当前线程进行中断操作
if ((runStateAtLeast(ctl.get(), STOP) ||
(Thread.interrupted() &&
runStateAtLeast(ctl.get(), STOP))) &&
!wt.isInterrupted())
wt.interrupt();
//执行任务,且在执行前后通过beforeExecute()和afterExecute()来扩展其功能
//这两个方法在当前类里面为空实现
try {
beforeExecute(wt, task);
Throwable thrown = null;
try {
task.run();
} catch (RuntimeException x) {
thrown = x; throw x;
} catch (Error x) {
thrown = x; throw x;
} catch (Throwable x) {
thrown = x; throw new Error(x);
} finally {
afterExecute(task, thrown);
}
} finally {
//帮助gc
task = null;
//完成任务数+1
w.completedTasks++;
w.unlock();
}
}
completedAbruptly = false;
} finally {
//自旋操作被退出,说明线程池正在结束
processWorkerExit(w, completedAbruptly);
}
}