上一篇我們簡單介紹了Java線程池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在構造完成後,便可以提交線程任務,提交方法主要有execute和submit。先介紹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(); //獲取線程池狀態
if (workerCountOf(c) < corePoolSize) { //工作線程數如果小於核心線程數
if (addWorker(command, true)) //構造worker,添加線程並啓動
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)) //如果阻塞隊列滿,可能還沒到最大線程數,所以嘗試增加一個worker
reject(command);
}
首先,線程池會獲取自身狀態,這裏,線程池的狀態由AtomicInteger變量ctl保存,ctl的高三位用來保存運行狀態,第三位保存workerCount,也就是當前的有效線程數。因此,workerCount的上限是(2^29-1)。
-1:RUNNING; 0:SHUTDOWN; 1:STOP; 2:TIDYING; 3:TERMINATED
其次,理解Worker和Task的區別。Worker是Task的封裝,構造它時,會裝入task並創建一個線程。該線程在運行完task後,會從Queue中獲取新的task運行。上述代碼中,第一步和第三步會直接創建Worker來運行滿足條件的task,第二步的task會進入阻塞隊列等待線程池中的Worker調取。
核心方法: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.
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
}
}
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;
}
首先,addWorker對線程池狀態進行了檢測:
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;
這裏,是對線程池狀態的判定。
- 如果狀態爲RUNNING(0),則直接跳過。
- 如果狀態爲STOP、DITYING或者TERMINATED(rs>=SHUTDOWN && !(rs == SHUTDOWN)),那麼不接受新線程,返回false。
- 如果狀態爲>=SHUTDOWN,同時 firstTask != null,那麼拒絕新線程。如果firstTask == null,那麼可能是增加新線程來消耗Queue中的線程。但是同時還要檢測queue是否爲空,如果爲空,那麼隊列已空,不需要增加消耗線程,如果隊列沒有空,那麼可以將null插入隊列中來清空隊列。
for (;;) {
int wc = workerCountOf(c); //獲取工作線程數
if (wc >= CAPACITY || //CAPACITY是ctl允許的最大工作線程數(2^29-1),不允許超出,否則導致狀態變量出錯
wc >= (core ? corePoolSize : maximumPoolSize)) //core爲true 則判斷最大核心線程數 否則最大線程數
return false; //如果超出規定的最大線程數 返回false
if (compareAndIncrementWorkerCount(c)) //CAS增加Worker數量
break retry;
c = ctl.get(); // Re-read ctl
if (runStateOf(c) != rs) //查看狀態是否改變 如果改變需要重來
continue retry;
// else CAS failed due to workerCount change; retry inner loop
}
private final class Worker
extends AbstractQueuedSynchronizer
implements Runnable
Worker(Runnable firstTask) {
setState(-1); // inhibit interrupts until runWorker 初始化 鎖爲-1
this.firstTask = firstTask;
this.thread = getThreadFactory().newThread(this);
}
/** Delegates main run loop to outer runWorker */
public void run() {
runWorker(this);
}
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) { //Worker初始時是有task的 如果沒有,那就去隊列中取
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);
}
}
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;
}
}
}
三、終止線程
shutdown方法停止接收新的任務,會完成線程池和隊列中的任務。源碼如下:
public void shutdown() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess(); //判斷可否操作目標線程
advanceRunState(SHUTDOWN); //設置線程池狀態SHUTDOWN
interruptIdleWorkers(); //中斷所有空閒線程
onShutdown(); // hook for ScheduledThreadPoolExecutor
} finally {
mainLock.unlock();
}
tryTerminate();
}
shutdown完成了這幾件事:
1)檢查能否操作線程
2)將線程池狀態轉爲SHUTDOWN
3)中斷所有空閒線程
shutdownNow會立刻停止接收新任務,且不再從隊列中獲取任務,而且停止正在運行的線程。
public List<Runnable> shutdownNow() {
List<Runnable> tasks;
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
advanceRunState(STOP); //設置狀態
interruptWorkers();
tasks = drainQueue();
} finally {
mainLock.unlock();
}
tryTerminate();
return tasks;
}
它會中斷所有線程。而不是空閒線程。然後拋棄隊列中的所有任務。