一、線程池工廠Executors
我們平時在使用線程池的時候一般都是通過Executors的newXxxxxPool()靜態(tài)方法來獲得不同功能的線程池對象。我們來看一下這些方法都是怎么創(chuàng)建線程池的:
/**
* 固定線程數(shù)的線程池
*/
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory);
}
/**
* 只有一個線程的線程池
*/
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
/**
* 可變大小線程池
*/
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
/**
*定時執(zhí)行的線程池
*/
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
我們可以看到這newFixedThreadPool()、newSingleThreadExecutor()、newCachedThreadPool()都是創(chuàng)建了一個ThreadPoolExecutor對象,而newScheduledThreadPool()則是創(chuàng)建了一個ScheduledThreadPoolExecutor對象,其實ScheduledThreadPoolExecutor也是繼承了ThreadPoolExecutor這個類,通過在ThreadPoolExecutor上擴展實現(xiàn)了定時執(zhí)行線程的功能。
ThreadPoolExecutor
我們先來看一下ThreadPoolExecutor的構(gòu)造方法:
/**
* 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,
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;
}
corePoolSize:線程池里最小線程數(shù)
maximumPoolSize:線程池里最大線程數(shù)
keepAliveTime:空閑線程存活的時間,也就是線程池的線程數(shù)超過corePoolSize后,空閑線程可以存活的時間,超過這個時間就會被銷毀。
unit: keepAliveTime的單位
workQueue:用來存放等待任務(wù)的隊列。這個隊列是個阻塞隊列
threadFactory:用來產(chǎn)生線程池里的線程的工廠
handler:當任務(wù)超過最大允許的任務(wù)數(shù)量后,新來任務(wù)的拒絕策略。
知道了上面幾個參數(shù),我們對ThreadPoolExecutor應(yīng)該有所了解,對Executors產(chǎn)生的不同功能的線程池也應(yīng)該有所了解。我們接下來討論一下ThreadPoolExecutor實現(xiàn)線程池的原理。
首先從提交任務(wù)的方法開始:
/**
* 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 {@code RejectedExecutionHandler}.
*執(zhí)行給定的任務(wù),這個任務(wù)可能在一個新的線程里執(zhí)行,也可能在一個已經(jīng)存在的線程里執(zhí)行
*如果任務(wù)不能被提交,不管是因為executor被shutdown還是因為容量到達界限,任務(wù)都會被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.
*/
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);
}
首先看到這一行代碼:int c = ctl.get(); ctl是什么呢?我們來看一下關(guān)于ctl的定義:
/**
* The main pool control state, ctl, is an atomic integer packing
* two conceptual fields
* workerCount, indicating the effective number of threads
* runState, indicating whether running, shutting down etc
*
* In order to pack them into one int, we limit workerCount to
* (2^29)-1 (about 500 million) threads rather than (2^31)-1 (2
* billion) otherwise representable. If this is ever an issue in
* the future, the variable can be changed to be an AtomicLong,
* and the shift/mask constants below adjusted. But until the need
* arises, this code is a bit faster and simpler using an int.
*
* The workerCount is the number of workers that have been
* permitted to start and not permitted to stop. The value may be
* transiently different from the actual number of live threads,
* for example when a ThreadFactory fails to create a thread when
* asked, and when exiting threads are still performing
* bookkeeping before terminating. The user-visible pool size is
* reported as the current size of the workers set.
*
* The runState provides the main lifecycle control, taking on values:
*
* RUNNING: Accept new tasks and process queued tasks
* SHUTDOWN: Don't accept new tasks, but process queued tasks
* STOP: Don't accept new tasks, don't process queued tasks,
* and interrupt in-progress tasks
* TIDYING: All tasks have terminated, workerCount is zero,
* the thread transitioning to state TIDYING
* will run the terminated() hook method
* TERMINATED: terminated() has completed
*
* The numerical order among these values matters, to allow
* ordered comparisons. The runState monotonically increases over
* time, but need not hit each state. The transitions are:
*
* RUNNING -> SHUTDOWN
* On invocation of shutdown(), perhaps implicitly in finalize()
* (RUNNING or SHUTDOWN) -> STOP
* On invocation of shutdownNow()
* SHUTDOWN -> TIDYING
* When both queue and pool are empty
* STOP -> TIDYING
* When pool is empty
* TIDYING -> TERMINATED
* When the terminated() hook method has completed
*
* Threads waiting in awaitTermination() will return when the
* state reaches TERMINATED.
*
* Detecting the transition from SHUTDOWN to TIDYING is less
* straightforward than you'd like because the queue may become
* empty after non-empty and vice versa during SHUTDOWN state, but
* we can only terminate if, after seeing that it is empty, we see
* that workerCount is 0 (which sometimes entails a recheck -- see
* below).
*/
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; }
注釋已經(jīng)很清楚告訴我們ctl是workerCount和runSate的結(jié)合。我們可以看到,線程池的容量是CAPACITY(線程池中允許的最大線程數(shù)是CAPACITY),也就是2的Integer.SIZE-3次方減一。ctl用低29位表示線程池中的線程數(shù),用剩下的高3位表示線程池的運行狀態(tài)。這一點大家要理解清楚。這下面三個方法是對ctl的操作
private static int runStateOf(int c) { return c & ~CAPACITY; } //獲取高三位,也就是線程池的運行狀態(tài)
private static int workerCountOf(int c) { return c & CAPACITY; } //獲取低29位,也就是線程池線程的數(shù)量
private static int ctlOf(int rs, int wc) { return rs | wc; } //生成ctl
理解了這個之后,我們繼續(xù)回到execute方法,當一個任務(wù)被提交給線程池后,分三種情況:
1、當前線程池中線程的數(shù)量小于corePoolSize,這個時候我們直接創(chuàng)建一個新線程來執(zhí)行提交的任務(wù)。
2、當線程池中的線程數(shù)大于corePoolSize時,如果線程池的狀態(tài)是RUNNING狀態(tài),并且任務(wù)加到任務(wù)隊列成功,我們?nèi)匀灰俅螜z查一下線程池的狀態(tài),防止任務(wù)在添加到任務(wù)隊列的過程中線程池被停止。如果線程池沒有被停止,則調(diào)用addWorker方法嘗試再創(chuàng)建一個線程去處理任務(wù)隊列。這里只是去嘗試創(chuàng)建,并不一定能創(chuàng)建成功,具體addWorker的實現(xiàn)我們接下來會討論。
3、如果任務(wù)添加到任務(wù)隊列失敗,這個時候我們再次調(diào)用addWorker方法嘗試創(chuàng)建一個新線程來處理當前任務(wù),如果失敗,則說明線程池被shutdown或者線程池的任務(wù)隊列已經(jīng)滿了。
知道了一個任務(wù)被提交到線程池的處理流程之后,我們來看一下每個步驟的具體實現(xiàn)。首先是addWorker方法,我們來看一下具體實現(xiàn):
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;
}
首先進入retry循環(huán)體,這個循環(huán)體的功能是去判斷線程池是否可以新創(chuàng)建線程。首先線程池的狀態(tài)如果大于SHUTDOWN狀態(tài),就不允許新創(chuàng)建線程(STOP狀態(tài):不再接受新任務(wù)也不處理任務(wù)隊列里的任務(wù),中斷正在進行的任務(wù);TIDYING:所有的任務(wù)被結(jié)束,workerCount被設(shè)置為0,線程狀態(tài)被轉(zhuǎn)變成TIDYING將會調(diào)用terminated()鉤子方法;TERMINATED:線程調(diào)用完terminated()方法)。如果線程池的狀態(tài)是SHUTDOWN狀態(tài),因為我們通過executor方法傳進來的任務(wù)不是空,所以,這個時候會返回false,不回去了創(chuàng)建新的線程了。也就是說,只有線程池處于RUNNING的時候才有創(chuàng)建新線程的機會。然后判斷當前線程數(shù)是否超過了線程池的最大容量,如果是則返回false不允許創(chuàng)建。然后通過CAS操作將workerCount加一,如果成功則跳出循環(huán)創(chuàng)建線程池,如果失敗,再次判斷線程池的狀態(tài)和進入方法時的狀態(tài)是否一致,如果不一致則重新執(zhí)行retry循環(huán)體,如果一致,則重新判斷線程池容量,決定是否能夠創(chuàng)建新的線程。
如果通過以上判斷,允許創(chuàng)建新的線程,則新創(chuàng)建一個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);
}
// Lock methods
//
// The value 0 represents the unlocked state.
// The value 1 represents the locked state.
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) {
}
}
}
}
我們先看一下Worker的構(gòu)造方法:當創(chuàng)建Worker對象的時候,會通過我們之前設(shè)置的ThreadFactory的newThread方法來創(chuàng)建一個線程,并交給Worker對象持有。我們來看一下默認的線程池的實現(xiàn):
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
在調(diào)用該方法的時候,會把Worker對象本身傳入,我們可以看到Worker實現(xiàn)了Runnable接口。所以當線程啟動的時候會調(diào)用的是Worker的run()方法。而Worker的run()方法調(diào)用了外部類的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);
}
}
這個方法才是線程池處理任務(wù)的整個核心內(nèi)容,進入方法后,會進入一個循環(huán)體:首先獲取要執(zhí)行的任務(wù),如果當前Worker持有的任務(wù)不是空,獲取的就是該任務(wù),如果是空,就調(diào)用getTask()方法來獲取任務(wù)隊列里的任務(wù)。這個方法也是實現(xiàn)線程池中空閑線程銷毀的關(guān)鍵。我們來看一下它的內(nèi)部實現(xiàn):
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;
}
}
}
進入方法的時候先定義一個標識位timedOut,這個標識位用來表示從任務(wù)隊列中獲取任務(wù)是否超時。如果超時,說明這段時間沒有新任務(wù)過來,這個線程也就是空閑的,如果當前線程數(shù)大于corePoolSize,這個線程就會被銷毀。我們來看一下這個過程是怎么實現(xiàn)的:當設(shè)置標識位之后,進入一個循環(huán)體,來判斷當前的線程池的狀態(tài),如果當前線程池的狀態(tài)大于等于STOP,方法直接返回null,返回nulll是什么概念呢?我們回到runWorker方法看一下,當getTask()返回null的時候,while循環(huán)結(jié)束,執(zhí)行finall語句塊里的processWorkerExit方法。執(zhí)行完這個方法后線程就會結(jié)束,也就是這個線程會被銷毀。我們繼續(xù)回到getTask()方法,當前線程池的狀態(tài)大于等于STOP時,不管任務(wù)隊列里是否有任務(wù)都不會獲取到任務(wù),線程會被銷毀。當線程池狀態(tài)是RUNNING狀態(tài)的時候會繼續(xù)接下來的判斷,當線程池狀態(tài)是SHUTDOWN的時候要去判斷任務(wù)隊列是否為空,如果是空就返回null,銷毀線程,如果不是空繼續(xù)接下來的操作。
當進行完上面的判斷后,在設(shè)置一個標識位timed,這個標識位用來表示當獲取任務(wù)超時后是否需要銷毀線程。然后進入if ((wc > maximumPoolSize || (timed && timedOut))這個判斷,如果當前線程數(shù)大于maximumPoolSize,說明線程被創(chuàng)建多了,這個時候要銷毀線程,直接返回null。如果獲取任務(wù)超時(第一次進入這個循環(huán)的時候肯定不存在這種情況,因為timedOut標識位被設(shè)置成了false),并且當前線程池里面的線程數(shù)大于1(因為要保證線程池里必須至少有一個線程)、任務(wù)隊列是空的時候,返回null銷毀線程。
結(jié)束以上判斷的時候就要去任務(wù)隊列取任務(wù),如果timed標識位(表示當獲取任務(wù)超時后是否需要銷毀線程)是ture,就需要在給定時間內(nèi)獲取任務(wù),不然就會返回null,如果返回null,就設(shè)置timedOut標識位為ture,表示獲取任務(wù)超時,當前線程是空閑線程。等到下次循環(huán)的時候就會結(jié)束方法返回null。如果正常獲取任務(wù)就講任務(wù)返回。到此getTask()的分析結(jié)束,我們做一個小小的總結(jié):如果線程池狀態(tài)大于STOP,直接返回null銷毀線程;如果當前線程池狀態(tài)是SHUTDOWN并且任務(wù)隊列是空,返回null銷毀線程;如果不是以上兩種情況,再判斷線程池是否設(shè)置了空閑線程銷毀,如果是的話,并且從任務(wù)隊列中獲取任務(wù)超時,就返回null銷毀線程;如果不是就返回獲取的線程。
當獲取到任務(wù)之后,就去判斷當前線程池是否被stop,如果是,中斷當前線程,如果不是,就調(diào)用interrupted()方法取消中斷標志。這一步是用來防止成功獲取任務(wù)之后線程池被中斷。
當做完 以上檢查之后,調(diào)用beforeExecute(wt, task)方法,來執(zhí)行前置操作,這個方法是個模板方法,交由子類實現(xiàn)。之后會執(zhí)行任務(wù)的run方法,真正的執(zhí)行任務(wù)。執(zhí)行完任務(wù)之后會調(diào)用 afterExecute(task, thrown)方法來執(zhí)行后置操作,這個方法也是模板方法。執(zhí)行完之后,會再次去獲取任務(wù)執(zhí)行以上操作。getTask()方法返回null的時候,會調(diào)用processWorkerExit(w, completedAbruptly)方法,這個方法做了講當前的worker對象從線程池中去除等操作(有可能還會重新創(chuàng)建一個線程)。有興趣的同學(xué)可以看一下。
到此Worker分析結(jié)束,我們繼續(xù)回到addWorker方法,當我們創(chuàng)建一個Worker對象后,講worker對象添加到workers容器里。然后啟動worker對象持有的線程。也就是用來處理任務(wù)的線程。
到此,線程池添加任務(wù)、處理任務(wù)的分析結(jié)束。