ThreadPoolExecutor介紹
ThreadPoolExecutor是一個管理線程的一個類,可以有效的複用和控制線程。之前比較好奇,像這樣的池化技術是如何實現的,今天我們就來一起探究下。本文是在JDK8的源碼下進行閱讀的,由於是自己總結,所以太過基礎的東西不做贅述。
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;
}
- 我們可能知道阿里規範的話,讓我們直接自己使用構造函數定義ThreadPoolExecutor,目的就是清晰展示線程池的參數,防止線程池出現問題,比如內存溢出。
- corePoolSize代表核心線程數
- maximumPoolSize代表最大線程數
- keepAliveTime代表空閒線程最大存活時間
- unit代表時間單位
- workQueue代表任務存儲隊列
- threadFactory代表線程工廠
- handler代表任務拒絕策略
成員變量ctl介紹
先來看下源碼:
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
private static final int COUNT_BITS = Integer.SIZE - 3;
private static final int CAPACITY = (1 << COUNT_BITS) - 1;
// runState is stored in the high-order bits
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
private static int runStateOf(int c) { return c & ~CAPACITY; }
private static int workerCountOf(int c) { return c & CAPACITY; }
private static int ctlOf(int rs, int wc) { return rs | wc; }
這裏截取一篇文章的介紹:
COUNT_BITS表示ctl變量中表示有效線程數量的位數,這裏COUNT_BITS=29;
CAPACITY表示最大有效線程數,根據位運算得出COUNT_MASK=11111111111111111111111111111,這算成十進制大約是5億,在設計之初就已經想到不會開啓超過5億條線程,所以完全夠用了;
線程池狀態的位運算得到以下值:
RUNNING:高三位值111
SHUTDOWN:高三位值000
STOP:高三位值001
TIDYING:高三位值010
TERMINATED:高三位值011
在多線程的環境下,運行狀態和有效線程數量往往需要保證統一,不能出現一個改而另一個沒有改的情況,如果將他們放在同一個AtomicInteger中,利用AtomicInteger的原子操作,就可以保證這兩個值始終是統一的。
- 這裏有點疑惑的可能是runStateOf、workerCountOf三個方法
- runStateOf其實是截取高三位,代表線程池狀態
- workerCountOf是低29位,代表線程個數
execute和submit方法源碼
介紹完一些參數後,我們來看看是如何運行的,這裏先看下submit方法源碼:
public <T> Future<T> submit(Callable<T> task) {
if (task == null) throw new NullPointerException();
RunnableFuture<T> ftask = newTaskFor(task);
execute(ftask);
return ftask;
}
- 這裏就是比對execute方法會有值返回,我們不做深究,直接來看execute方法源碼
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
if (! isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
else if (!addWorker(command, false))
reject(command);
}
- 運行邏輯是,如果傳入線程爲null,直接拋出異常,之後再判斷工作線程是否小於corePoolSize,然後就是addWorker方法,關於這個方法源碼我們之後解析
- 如果大於等於corePoolSize,就會在workQueue也就是構造函數中傳入的阻塞隊列中添加,如果成功,下面一些判斷條件主要是防禦性檢查
- 最後如果沒添加進workQueue中,會再次嘗試addWorker,不過這是針對的是maximumPoolSize,不行也會直接拒絕
- 這裏涉及到的addWorker、reject源碼我們稍後來看
reject方法源碼
final void reject(Runnable command) {
handler.rejectedExecution(command, this);
}
- 這裏比較簡單,就是調用拒絕策略,不做過多分析
addWorker方法源碼
private boolean addWorker(Runnable firstTask, boolean core) {
//<1>
retry:
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
// Check if queue empty only if necessary.
if (rs >= SHUTDOWN &&
! (rs == SHUTDOWN &&
firstTask == null &&
! workQueue.isEmpty()))
return false;
for (;;) {
int wc = workerCountOf(c);
if (wc >= CAPACITY ||
wc >= (core ? corePoolSize : maximumPoolSize))
return false;
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
}
}
//<2>
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;
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)) {
if (t.isAlive()) // precheck that t is startable
throw new IllegalThreadStateException();
workers.add(w);
int s = workers.size();
if (s > largestPoolSize)
largestPoolSize = s;
workerAdded = true;
}
} finally {
mainLock.unlock();
}
if (workerAdded) {
t.start();
workerStarted = true;
}
}
} finally {
if (! workerStarted)
addWorkerFailed(w);
}
return workerStarted;
}
- 第<1>塊內容,主要是針對ctl加一,來代表工作線程數量
- 其中的core參數是來決定是否是核心線程
- 第<2>塊內容是真實添加Worker,首先會創建Worker,其次拿出worker中的Thread,注意,這裏的Thread不是我們初始化Worker中的firstTask,具體我們等下來看,然後會把創建的worker加到workers中,最後啓動線程
- 這裏會有疑問,那線程池是如何控制的呢?答案我們進入Worker來尋找
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;
/**
* Creates with given first task and thread from ThreadFactory.
* @param firstTask the first task (null if none)
*/
Worker(Runnable firstTask) {
setState(-1); // inhibit interrupts until runWorker
this.firstTask = firstTask;
this.thread = getThreadFactory().newThread(this);
}
/** Delegates main run loop to outer runWorker */
public void run() {
runWorker(this);
}
...
}
- 這裏看到我們運行的線程是通過傳入的threadFactory來創建的
- 運行這個線程,我們來看下run方法實際是跑了runWorker方法,我們接下來看下源碼
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 {
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 {
task = null;
w.completedTasks++;
w.unlock();
}
}
completedAbruptly = false;
} finally {
processWorkerExit(w, completedAbruptly);
}
}
- 這裏主要的邏輯就是while不斷循環,然後通過getTask方法去取相應任務,持續運行,如果取到的task爲null,則會結束線程。注意這裏的task在調用run方法之後會直接設置爲null,代表清除,那麼重要的就是getTask是怎麼控制任務的了,我們來看下
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 {
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
workQueue.take();
if (r != null)
return r;
timedOut = true;
} catch (InterruptedException retry) {
timedOut = false;
}
}
}
- 前面一堆,是用來檢查線程池狀態的,之後有一段很關鍵的代碼,是來控制核心線程和其它線程區別的
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
,這段代碼的意思是如果大於核心線程,則爲true,具體作用我們往下看 - 在
Runnable r = timed ? workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) : workQueue.take();
中我們可以看到這裏我們是通過阻塞隊列來分別的,如果是核心線程,我們會通過阻塞隊列的task方法阻塞着,如果不是則通過poll定時,超時空閒時間沒有取到任務則代表是空閒的,返回timedOut = true; - 這時再看上面一段代碼邏輯
((wc > maximumPoolSize || (timed && timedOut)) && (wc > 1 || workQueue.isEmpty()))
會返回true,則方法最終會返回null,最終也會結束外層的線程。
個人總結
源碼看的差不多了,到了總結環節,畢竟不是爲了看源碼而看源碼,那麼ThreadPoolExecutor是如何協同運作的呢?
個人認爲主要還是通過Worker這個內部類來進行協作的,我們可以理解線程池執行的線程內存,其實可以理解爲傳入了一個執行方法而已,並且都有一個實現類,這時其實傳入的線程實現類本身的線程意義就沒有了,其實就是一個方法,線程的話是通過線程池的內部屬性。然後具體的管控任務,空閒線程的回收,是通過阻塞隊列的take和poll超時方法來控制的。大概線程池遇到對的問題就這些,如果有大家有其他問題,也歡迎交流。
今日知識圖譜: