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());
}
}