ThreadPoolExecutor提交任务入口代码如下:
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
// ctl高3位表示线程池状态,低29位表示线程数目,对ctl的访问需要进行位运算
int c = ctl.get();
// 如果worker线程数目小于corePoolSize,增加一个worker线程
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
// 如果worker线程不小于corePoolSize,并且线程池正在运行,则把任务添加到workQueue中
// workQueue是一个BlockingQueue
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
// 再次获取线程池状态码并检测线程池是否运行,如果没有运行,则移除刚才提交的任务,调用reject方法
// reject方法可以由RejectedExecutionHandler指定
if (! isRunning(recheck) && remove(command))
reject(command);
// 如果worker线程数目为0,以maximumPoolSize为限制增加一个worker线程
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
// 如果任务添加到workQueue(有可能是有界队列)失败,
// 以maximumPoolSize为限制增加一个worker线程
// 如果增加线程失败,调用reject方法
else if (!addWorker(command, false))
reject(command);
}
看完任务如何提交,继续看addWorker方法是怎么运行的,只留下了方法的主干:
private boolean addWorker(Runnable firstTask, boolean core) {
retry:
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
// 检测条件
if (rs >= SHUTDOWN &&
! (rs == SHUTDOWN &&
firstTask == null &&
! workQueue.isEmpty()))
return false;
for (;;) {
int wc = workerCountOf(c);
// 如果worker线程数目大于CAPACITY(ctl的低29位全为1,500多万)
// 或者worker线程大于corePoolSize,maximumPoolSize(用core开关控制)
// 添加worker线程失败
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;
}
}
// 增加worker线程测试条件通过,真正开始增加worker线程
boolean workerStarted = false;
boolean workerAdded = false;
Worker w = null;
try {
// new一个Worker,Worker是对线程和任务的一个封装,下面会讲到
w = new Worker(firstTask);
final Thread t = w.thread;
if (t != null) {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
// 在workers里面新增worker
workers.add(w);
int s = workers.size();
// 更新largestPoolSize
if (s > largestPoolSize)
largestPoolSize = s;
workerAdded = true;
}
} finally {
mainLock.unlock();
}
// 如果worker添加成功,则启动这个worker线程
if (workerAdded) {
t.start();
workerStarted = true;
}
}
} finally {
// 如果启动worker失败,调用addWorkerFailed方法
// 这个方法主要是减少worker数目,从workers里面移除刚才添加的worker
// 并在线程池中尝试中断一个idle worker
if (! workerStarted)
addWorkerFailed(w);
}
return workerStarted;
}
下面接着看Worker是怎么对线程与任务封装的,下面是Worker类的代码:
// Worker类本身也是一个Runnable
// AQS可以这样理解,它内部持有一个状态,并发的线程可以原子性的去修改这个状态
private final class Worker
extends AbstractQueuedSynchronizer
implements Runnable
{
// 运行任务的线程
final Thread thread;
// 初始化运行的任务,可以为null
Runnable firstTask;
// 完成任务的计数
volatile long completedTasks;
Worker(Runnable firstTask) {
setState(-1); // 状态设置为-1,禁止没有start线程前去中断这个线程
this.firstTask = firstTask;
// 传的是this对象,所以线程start会调用this的run方法
this.thread = getThreadFactory().newThread(this);
}
// run方法委托给runWorker方法,参数传的是this
public void run() {
runWorker(this);
}
// 关于锁的方法
// 通过lock与unlock,可以知道worker是不是idle worker
// 0代表没有加锁状态
// 1代表加锁状态
protected boolean isHeldExclusively() {
return getState() != 0;
}
protected boolean tryAcquire(int unused) {
if (compareAndSetState(0, 1)) {
setExclusiveOwnerThread(Thread.currentThread());
return true;
}
return false;
}
protected boolean tryRelease(int unused) {
setExclusiveOwnerThread(null);
setState(0);
return true;
}
public void lock() { acquire(1); }
public boolean tryLock() { return tryAcquire(1); }
public void unlock() { release(1); }
public boolean isLocked() { return isHeldExclusively(); }
// 线程运行后也可以通过这个方法中断线程的运行
void interruptIfStarted() {
Thread t;
if (getState() >= 0 && (t = thread) != null && !t.isInterrupted()) {
try {
t.interrupt();
} catch (SecurityException ignore) {
}
}
}
}
下面来看看runWorker方法,只留下了方法的主干:
final void runWorker(Worker w) {
Thread wt = Thread.currentThread(); //获取到worker的thread
Runnable task = w.firstTask;
w.firstTask = null;
w.unlock(); // unlock worker,允许中断thread
boolean completedAbruptly = true;
// 死循环,当task为null或者从任务队列获取任务为null时,worker的thread会退出
while (task != null || (task = getTask()) != null) {
w.lock(); // lock表示这个worker不是idle worker
// 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();
try {
Throwable thrown = null;
try {
// 运行任务
task.run();
} catch (Throwable x) {
thrown = x;
throw new Error(x);
}
} finally {
task = null;
w.completedTasks++;
w.unlock();
}
}
}
下面接着看worker线程是如何从任务队列获取任务的:
private Runnable getTask() {
boolean timedOut = false; // Did the last poll() time out?
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
// 如果线程池状态至少为shutdown,并且
// 线程池状态至少为stop或者工作队列为空
// 减少worker的数目,并返回null,当worker线程检测到任务为null时,会自己退出
if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
decrementWorkerCount();
return null;
}
int wc = workerCountOf(c);
// 当allowCoreThreadTimeOut为true或者worker线程数目大于corePoolSize时,允许超时
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
// 如果worker线程数目大于maximumPoolSize,或者
// 允许超时并且上一次poll也超时,并且
// worker线程数目大于1,或者工作队列为空
// 减少worker线程数目返回null
if ((wc > maximumPoolSize || (timed && timedOut))
&& (wc > 1 || workQueue.isEmpty())) {
if (compareAndDecrementWorkerCount(c))
return null;
continue;
}
try {
// 允许超时则用poll,否则take
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
workQueue.take();
if (r != null)
return r;
timedOut = true;
} catch (InterruptedException retry) {
timedOut = false;
}
}
}
以上就是整个ThreadPoolExecutor主要的方法,还有另一个比较重要的方法,是用来清除idle worker的:
private void interruptIdleWorkers(boolean onlyOne) {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
for (Worker w : workers) {
Thread t = w.thread;
// 如果worker的thread没有中断,并且尝试去获取worker的锁
// 成功则表示worker没有运行任务,那么中断这个idle worker
// 失败则表示worker正在运行任务,不中断这个worker
if (!t.isInterrupted() && w.tryLock()) {
try {
t.interrupt();
} catch (SecurityException ignore) {
} finally {
w.unlock();
}
}
if (onlyOne)
break;
}
} finally {
mainLock.unlock();
}
}