一.參考
二.架構
三.源碼細節
(一).構造方法
ThreadPoolExecutor的構造方法只是給成員變量賦值,沒有多餘邏輯.主要成員變量是corePoolSize,maximumPoolSize,workQueue,keepAliveTime線程允許的空閒時間,threadFactory,RejectedExecutionHandler(拒絕策略有四種,CallerRunsPolicy,默認AbortPolicy,DiscardPolicy,DiscardOldestPolicy).
下面源碼調試以ThreadPoolExecutor(3, 6, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<>(4))爲例.
(二).執行任務.
進入ThreadPoolExecutor#execute.代碼如下:
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();
//如果線程池內的正在執行的線程數量(成員變量ctl的低29位的值,Worker的數量)小於corePoolSize,則創建線程
if (workerCountOf(c) < corePoolSize) {
//如果線程池內的正在執行的線程數量小於corePoolSize,創建線程執行
if (addWorker(command, true))
//線程池內的正在執行的線程數量小於corePoolSize時,這裏直接返回.
return;
c = ctl.get();
}
//如果線程池內線程數量大於等於corePoolSize,新來的線程入隊列.比如第4,5個線程.
//進入ArrayBlockingQueue#offer()入隊.隊列長度爲前面構造函數設置的4.
//隊列滿了,則不進入這個分支
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
//如果此時線程池不是running狀態,則從隊列移除.double check
if (! isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)
//如果正在執行的線程數爲0時,添加工作線程.
addWorker(null, false);
}
//如果線程池內線程數量小於等於corePoolSize+max(隊列長度,maxPoolSize),則入隊列
//大於時,執行拒絕策略
else if (!addWorker(command, false))
//線程池的線程數量大於corePoolSize+max(隊列長度,maxPoolSize)時,執行拒絕策略
reject(command);
}
(二).創建線程執行
private boolean addWorker(Runnable firstTask, boolean core) {
retry:
//兩層死循環
for (;;) {
int c = ctl.get();
//獲取當前線程池的狀態
int rs = runStateOf(c);
// Check if queue empty only if necessary.
//線程池不是running狀態且(線程池關閉或者隊列爲空),則不創建新線程
if (rs >= SHUTDOWN &&
! (rs == SHUTDOWN &&
firstTask == null &&
! workQueue.isEmpty()))
return false;
for (;;) {
int wc = workerCountOf(c);
//CAPACITY高3位爲0,低29位全是1,即29位的最大整數。如果線程數量大於大於這個數,則退出不創建線程,基本不可能滿足.
//core進來時,如果是corePoolSize這步判斷進來是寫死的true.如果是maxPoolSize那步,進來是false.取maximumPoolSize
//如果線程數量大於等於corePoolSize或者maximumPoolSize,退出不創建線程
if (wc >= CAPACITY ||
wc >= (core ? corePoolSize : maximumPoolSize))
return false;
//走到這一步正常去創建Worker線程
//對成員變量ctl的值加1,即線程數量+1
if (compareAndIncrementWorkerCount(c))
break retry;
c = ctl.get(); // Re-read ctl
//如果當前線程池狀態和剛開始進來外層for循環時的狀態不一樣,重新判斷是否能創建線程
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 {
//Worker內部封裝了線程,創建線程,Worker構造方法中new Thread
w = new Worker(firstTask);
final Thread t = w.thread;
if (t != null) {
//獲取線程池的主鎖,執行這段代碼的線程是主線程。難道可以多個主線程操作同一個線程池,做併發控制?
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
// Recheck while holding lock.
// Back out on ThreadFactory failure or if
// shut down before lock acquired.
//獲取當前線程池狀態,第二次檢查前面兩層for循環的最後做的第一次檢查
int rs = runStateOf(ctl.get());
//線程池是running狀態,才添加
//線程池是shutdown狀態,但是隊列中有未完成的任務,也可以添加.
if (rs < SHUTDOWN ||
(rs == SHUTDOWN && firstTask == null)) {
//新創建的線程已經被啓動.則拋異常.這裏誰會啓動它?
if (t.isAlive()) // precheck that t is startable
throw new IllegalThreadStateException();
//添加worker到成員變量HashSet中
workers.add(w);
int s = workers.size();
//修改最大線程池大小
if (s > largestPoolSize)
largestPoolSize = s;
workerAdded = true;
}
} finally {
mainLock.unlock();
}
if (workerAdded) {
//運行新加的線程代碼.進入ThreadPoolExecutor#runWorker
t.start();
workerStarted = true;
}
}
} finally {
//線程啓動失敗了.從隊列中移除這個Worker,減少線程計數,結束該失敗線程
if (! workerStarted)
addWorkerFailed(w);
}
return workerStarted;
}
(三).Worker工作線程類
private final class Worker
extends AbstractQueuedSynchronizer
implements Runnable
{
/**
* This class will never be serialized, but we provide a
* serialVersionUID to suppress a javac warning.
*/
private static final long serialVersionUID = 6138294804551838833L;
/** Thread this worker is running in. Null if factory fails. */
//具體工作線程
final Thread thread;
/** Initial task to run. Possibly null. */
//執行的任務代碼
Runnable firstTask;
/** Per-thread task counter */
volatile long completedTasks;
...
}
構造方法中調用getThreadFactory().newThread(Runnable)創建線程.
Worker(Runnable firstTask) {
setState(-1); // inhibit interrupts until runWorker
this.firstTask = firstTask;
this.thread = getThreadFactory().newThread(this);
}
(四).線程代碼執行.
進入ThreadPoolExecutor#runWorker()方法.
final void runWorker(Worker w) {
//執行任務的子線程
Thread wt = Thread.currentThread();
Runnable task = w.firstTask;
w.firstTask = null;
w.unlock(); // allow interrupts
boolean completedAbruptly = true;
try {
//獲取要執行的任務
while (task != null || (task = getTask()) != null) {
//獲取任務的鎖
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();
try {
//在線程開始執行前,可以做一些攔截操作,類似aop,繼承ThreadPoolExecutor,實現這個方法
beforeExecute(wt, task);
Throwable thrown = null;
try {
//執行Runnable任務的run方法代碼
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 {
task = null;
//當前Worker執行任務數+1
w.completedTasks++;
w.unlock();
}
}
completedAbruptly = false;
} finally {
processWorkerExit(w, completedAbruptly);
}
}
(五).線程池狀態和內部的線程數量
CAPACITY的值爲0b00011111_11111111_11111111_11111111.參數c就是傳進來的成員變量AtomicInteger ctl的值.它的高3位存線程池狀態,低29位存線程池內的線程數量。
private static int runStateOf(int c) {
//取高3位的值
return c & ~CAPACITY;
}
private static int workerCountOf(int c) {
//取低29位的值
return c & CAPACITY;
}
線程池的幾種狀態,整數值越來越大:
RUNNNING:0b11100000_00000000_00000000_00000000.十進制表示爲-536870912.
SHUTDOWN:0
STOP:0b00100000_00000000_00000000_00000000.十進制爲536870912.
TIDYING:0b01000000_00000000_00000000_00000000.
TERMINATED:0b01100000_00000000_00000000_00000000.
(六).從隊列獲取任務getTask()
private Runnable getTask() {
boolean timedOut = false; // Did the last poll() time out?
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
// Check if queue empty only if necessary.
//線程池關閉或者(線程池停止,且沒有未完成的隊列任務),則返回沒有任務
if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
decrementWorkerCount();
return null;
}
int wc = workerCountOf(c);
// Are workers subject to culling?
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
//線程數量超過最大數量限制,則不再取任務
if ((wc > maximumPoolSize || (timed && timedOut))
&& (wc > 1 || workQueue.isEmpty())) {
if (compareAndDecrementWorkerCount(c))
return null;
continue;
}
try {
//從任務隊列中阻塞式的取出一個任務去執行.keepAliveTime時間內沒有取到任務,返回for循環繼續執行
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
workQueue.take();
if (r != null)
return r;
timedOut = true;
} catch (InterruptedException retry) {
timedOut = false;
}
}
}
(七).Worker線程任務退出時清理
private void processWorkerExit(Worker w, boolean completedAbruptly) {
if (completedAbruptly) // If abrupt, then workerCount wasn't adjusted
decrementWorkerCount();
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
//完成任務數加一
completedTaskCount += w.completedTasks;
//從HashSet中移除這個Worker
workers.remove(w);
} finally {
mainLock.unlock();
}
//判斷是否需要停止線程池
tryTerminate();
int c = ctl.get();
if (runStateLessThan(c, STOP)) {
if (!completedAbruptly) {
int min = allowCoreThreadTimeOut ? 0 : corePoolSize;
if (min == 0 && ! workQueue.isEmpty())
min = 1;
if (workerCountOf(c) >= min)
return; // replacement not needed
}
addWorker(null, false);
}
}