Java執行緒池ThreadPoolExecutor使用與解析
概述
JDK提供了一個工具類Executors來非常方便的建立執行緒池,下面主要通過一個示例來分析Java執行緒池的實現原理。
使用
Runnable runnable = new Runnable() { @Override public void run() { // do something } }; ExecutorService executorService = Executors.newFixedThreadPool(2); executorService.submit(runnable); executorService.shutdown();
例子裡面使用了Executors.newFixedThreadPool(2)建立了一個固定只有2個執行緒的執行緒池,返回了一個ExecutorService物件,然後呼叫executorService.submit()方法來啟動一個執行緒,最後呼叫executorService.shutdown()來關閉執行緒池。
使用起來非常的方便,接下來通過深入原始碼看一下背後的原理。
原始碼分析
ExecutorService
看一下ExecutorService的定義
public interface ExecutorService extends Executor { void shutdown(); List<Runnable> shutdownNow(); boolean isShutdown(); boolean isTerminated(); boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException; <T> Future<T> submit(Callable<T> task); <T> Future<T> submit(Runnable task, T result); Future<?> submit(Runnable task); ... }
ExecutorService繼承自Executor
public interface Executor {
void execute(Runnable command);
}
列出了一部分的介面,主要是提供了幾個啟動執行緒執行執行緒任務的方法,接收不同的引數,以及關閉執行緒池的方法。submit方法接收Runnable或者Callable方法,返回一個Future物件用於非同步獲取執行結果。execute方法只接收一個Runnable引數,並且沒有返回值。
Executors
再看一下Executors工具類的定義
public class Executors { public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); } public static ExecutorService newWorkStealingPool() { return new ForkJoinPool (Runtime.getRuntime().availableProcessors(), ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); } 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>()); } ... }
大致是這個樣子的,這裡列出了一部分,提供了建立固定執行緒數的執行緒池(newFixedThreadPool),工作竊取的執行緒池(newWorkStealingPool),單個執行緒的執行緒池(newSingleThreadExecutor),不知道怎麼稱呼的執行緒池(newCachedThreadPool)。
ThreadPoolExecutor
宣告
以FixedThreadPool為例一探究竟,看一下FixedThreadPool返回的 ThreadPoolExecutor究竟是什麼東西
public class ThreadPoolExecutor extends AbstractExecutorService {...}
public abstract class AbstractExecutorService implements ExecutorService {
...
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
return new FutureTask<T>(callable);
}
public Future<?> submit(Runnable task) {
if (task == null) throw new NullPointerException();
RunnableFuture<Void> ftask = newTaskFor(task, null);
execute(ftask);
return ftask;
}
public <T> Future<T> submit(Callable<T> task) {
if (task == null) throw new NullPointerException();
RunnableFuture<T> ftask = newTaskFor(task);
execute(ftask);
return ftask;
}
...
}
ThreadPoolExecutor繼承自AbstractExecutorService,AbstractExecutorService是一個實現了ExecutorService的抽象類。
抽象類中提供了submit方法的具體實現,將傳入的Runnable或者Callable方法通過newTaskFor方法轉換成一個FutureTask物件(它是RunnableFuture)的實現類,然後呼叫父類的execute方法執行任務,最終返回runnableFuture物件。從這可以看出來ExecutorService.submit()方法內部還是通過呼叫Executor.execute()方法來執行的,只是將引數轉換成一個Future物件,通過Future物件來獲取執行結果。
內部結構
/**
* 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
*/
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; }
定義了5種執行緒池的狀態
- RUNNING 表示執行緒池可以接受新的任務
- SHUTDOWN 表示不接受新的任務,但是繼續處理佇列中的任務
- STOP 表示不接受新的任務,中斷當前處理的任務和佇列種的任務
- TIDYING 表示所有任務都已經中止了,所有執行緒都停止了,將要執行 terminated()方法之前的狀態
- TERMINATED 表示 terminated()方法已經執行完了
有5種狀態變化的流程
- RUNNING -> SHUTDOWN 呼叫了 shutdown()
- (RUNNING or SHUTDOWN) -> STOP 呼叫了 shutdownNow()
- SHUTDOWN -> TIDYING 等待處理的佇列和執行緒池都為空的時候
- STOP -> TIDYING 執行緒池為空還沒有執行terminated()之前的狀態
- TIDYING -> TERMINTED 已經執行完terminated()方法
AtomicInteger型別的ctl變數存著當前worker(Worker是一個內部類,下面會詳細解釋)的數量。
ThreadPoolExecutor用一個32位整型的高3位表示執行的狀態,剩下的29位表示可以支援的執行緒數。
COUNT_BITS 為32-3 = 29, 比如 RUNNING 是 -1 << COUNT_BITS,即-1帶符號位左移29位,就是101000...0,STOP為001000...0,TIDYING為010000...0。
CAPACITY 為 (1 << COUNT_BITS) - 1,1左移29位之後-1,最後的結果位 0001111...1,最高3位是0 剩下的29位都是1。
workerCountOf(int c) 用來計算當前執行緒數,用的方法是 c & CAPACITY 即 c & 0001111...1,取除了高3位的剩下29位來判斷。
runStateOf(int c) 用來檢視當前的執行緒狀態, c & ~CAPACITY 即 c & 1110000...0,取高3位來判斷。
private final BlockingQueue<Runnable> workQueue;
private final ReentrantLock mainLock = new ReentrantLock();
private final HashSet<Worker> workers = new HashSet<Worker>();
private final Condition termination = mainLock.newCondition();
private int largestPoolSize;
private long completedTaskCount;
private volatile ThreadFactory threadFactory;
private volatile RejectedExecutionHandler handler;
private volatile long keepAliveTime;
private volatile boolean allowCoreThreadTimeOut;
private volatile int corePoolSize;
private volatile int maximumPoolSize;
private static final RejectedExecutionHandler defaultHandler = new AbortPolicy();
private static final RuntimePermission shutdownPerm = new RuntimePermission("modifyThread");
在來看一些其他的全域性屬性。workerQueue 一個BlockingQueue存放Runnable物件,workers 一個HashSet存放Worker物件,還有一些corePoolSize maximumPoolSize等就是平時配置連線池的引數。
構造方法
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
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.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
Executors提供的newFIxedThreadPool方法其實建立的是一個ThreadPoolExecutor物件,以 newFixedPoolSize(2) 為例,通過將corePoolSize maximumPoolSize都是設定為2來實現固定數量的執行緒池。keepAliveTime設定為0微秒。workerQueue傳入了一個LinkedBlockingQueue物件。
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))
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);
}
通讀幾遍程式碼加上上面的註釋,基本可以理解整個方法的意思。主要的思想是
- 當前執行緒數小於corePoolSize的時候呼叫addWorker來建立Worker類
- 當前執行緒數量大於corePoolSize的時候執行 workQueue.offer() 將任務加到等待佇列裡面
- 如果加不進等待佇列並且建立Worker失敗,就使用reject策略來拒絕當前任務
註釋中的第二點做了很多檢查,將任務加到等待佇列之後還要做一次檢檢視看是否需要建立Worker,防止之前建立的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;
}
第一個for迴圈檢查執行緒數有沒有超過corePoolSize或者maximunPoolSize。過了這個for迴圈之後就是建立Worker的地方了
private final class Worker extends AbstractQueuedSynchronizer implements Runnable {
/** 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);
}
public void run() {
runWorker(this);
}
...
}
Worker 類繼承自 AbstractQueuedSynchronizer 實現了 Runnable介面, AbstractQueuedSynchronizer 這個玩意特別厲害,是併發程式設計的核心類,由於內容非常多本文不作解析。Worker類中維護了一個Thread物件,存了當前執行的執行緒,還維護了一個Runnable物件(firstTask),存了當前執行緒需要執行的物件。再回顧addWorker方法,其實就是用傳入的firstTask引數建立一個Worker物件,並使worker物件啟動一個執行緒去執行firstTask。重點在Worker物件的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) {
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);
}
}
runWorker方法接受一個Worker引數,將引數裡面的firstTask拿出來,然後呼叫 task.run() 方法直接執行這個task,執行完將task變數設定為null。然後這裡有一個while迴圈 while (task != null || (task = getTask()) != null),當task等於null的時候呼叫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;
}
}
}
getTask()方法裡面有一個死迴圈,boolean timed = allowCoreThreadTimeOut || wc > corePoolSize; timed變數判斷wc變數是否大於corePoolSize (allowCoreThreadTimeOut 預設為 false)。然後下面有一行程式碼判斷timed時候為ture,如果為true,呼叫 workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS),否則呼叫workQueue.take(),從等待佇列中獲取等待被處理的執行緒,然後返回出去。poll和take的區別是 當佇列裡面沒有資料的時候poll馬上返回false,而take會堵塞當前執行緒直到佇列裡面有資料。這裡解釋了為什麼執行緒池能夠維持執行緒不釋放。
總結
當設定了corePoolSize的時候,這個引數代表了能夠執行的執行緒數,當用戶執行submit方法的時候首先會去判斷當前執行緒數有沒有達到corePoolSize,如果沒有達到,就建立Worker物件並啟動執行緒執行任務,一個物件內維護一個執行緒,當執行緒數超過corePoolSize的時候,使用者執行submit方法的時候只是將任務放到等待佇列裡面,核心執行緒不斷從等待佇列裡面取出任務執行,沒有任務的時候一直被堵塞住,當有任務來的時候直接取出執行,避免了不斷建立執行緒帶來的開銷,以及增加了系統