線程池的相關類結構
線程池就是存儲了已創建指定個數的線程的集合,當需要用線程執行任務的時候,就可以從線程池中拿一個空閒的線程來執行任務。先看下源碼,源碼分析完了再看線程池的周邊應用,以及幾個思考。
第一部分,先看下線程池的結構,然後是如何創建線程池。
第二部分,線程池的狀態
第三部分,提交任務到線程池的方法
第四部分,關閉線程池
第五部分,拒絕策略
第一部分:
public class ThreadPoolExecutor extends AbstractExecutorService {
// 成員變量
// 任務隊列
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;
// 是否允許核心線程超時,默認false。false是當空閒時也保持活着狀態,true是核心線程使用
// keepAliveTime時間來控制等待任務的超時時間
private volatile boolean allowCoreThreadTimeOut;
// 核心線程池的大小
private volatile int corePoolSize;
// 最大的線程池大小
// private volatile int maximumPoolSize;
// 默認的拒絕策略是中斷,拋異常
private static final RejectedExecutionHandler defaultHandler = new AbortPolicy();
// 構造函數
/**
*
*corePoolSize:核心線程池大小,也就是說有這麼多線程一直活着,直到關閉線程池
*maximumPoolSize:最大的線程數,也就是說除了常駐的線程外,當阻塞隊列滿的時候,還可以新建
*maximumPoolSize-corePoolSize個線程來處理任務
*keepAliveTime:就是那些可擴展出來的空閒線程當空閒時的存活時間
*unit:是上面空閒時間的單位
*workqueue:工作隊列,當提交任務後,任務已經佔滿核心線程來,就會被添加到這個阻塞隊列裏,等待
*被線程執行
*threadfactory:創建線程的工廠,可以自己實現,也可以利用另一個構造函數使用默認的工廠
*handler:拒絕策略,當阻塞隊列滿時,這時候新添加任務進來後,對這個任務的拒絕策略,有4種
*/
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;
}
// 這個構造函數使用了默認的線程工廠和默認的拒絕策略,其他兩個構造函數是隻默認一個參數
// 默認的線程工廠就是創建的線程是非守護線程,優先級爲NORM_PRIORITY,詳情看後面介紹
// 默認的拒絕策略就是當阻塞隊列滿了,來了新任務,則拋出異常
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
// 其他兩個不看了...
}
Executors.defaultThreadFactory():
public static ThreadFactory defaultThreadFactory() {
return new DefaultThreadFactory();
}
ThreadPoolExecutor的內部類:
static class DefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
DefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "pool-" +
poolNumber.getAndIncrement() +
"-thread-";
}
// 新建線程時,設置線程爲非守護線程,優先級爲NORM_PRIORITY
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;
}
}
// 默認的拒絕策略:
private static final RejectedExecutionHandler defaultHandler =
new AbortPolicy();
public static class AbortPolicy implements RejectedExecutionHandler {
/**
* Creates an {@code AbortPolicy}.
*/
public AbortPolicy() { }
/**
* Always throws RejectedExecutionException.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
* @throws RejectedExecutionException always
*/
// 直接拋異常
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}
}
// 再看下用Executors創建線程池的方法(不建議用這種方式創建線程池),這個類的設計風格,突然想起
// effective Java裏面的靜態工廠方法創建對象:
public class Executors {
// 創建固定大小的線程池,空閒線程被立即回收,任務阻塞隊列是無界隊列,
// 最大可以是Integer.MAX_VALUE(問題在於這,可以一直提交任務進去,佔滿內存)
// 默認的線程工廠和拒絕策略
// 還有個重載方法,可以傳線程工廠進去
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
// 創建只有一個線程的線程池,問題與上面的fix一樣
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
// 創建一個可以無限增加線程數的線程池(問題也很大,想想線程數可以一直創建下去)
// 空閒線程超過60秒就回收
// 任務隊列是一個棧的形式(非公平)
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
// 創建一個可延遲執行的線程池,核心線程池爲1,線程池最大可達Integer.MAX_VALUE
// 可延遲的工作隊列
// 創建委託類管理已創建的可延遲的線程池
// 延遲相關的內容,下文還會講到
public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1));
}
}
第二部分,線程池的狀態:
// ctl記錄裏兩個信息:一個是線程池的狀態(高3位),一個是線程池的線程數量(低29位)
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
// 高3位值是111,此狀態能夠接受新任務,並且對已添加的任務處理
private static final int RUNNING = -1 << COUNT_BITS;
// 高3位值是000,此狀態不能接受新任務,但能處理已添加的任務
private static final int SHUTDOWN = 0 << COUNT_BITS;
// 高3位值是001,此狀態不能接受新任務,不能處理已添加的任務,並且會中斷正在執行的任務
private static final int STOP = 1 << COUNT_BITS;
// 高3位值是010,當所有任務終止時,線程池會變成TIDYING狀態,當線程池變爲TIDYING狀態時,會執
// 行鉤子函數terminated()。terminated()在ThreadPoolExecutor類中是空的,若用戶想在線程
// 池變爲TIDYING時,進行相應的處理;可以通過重載terminated()函數來實現。
private static final int TIDYING = 2 << COUNT_BITS;
// 高3位值是011,線程池徹底終止就變成TERMINNATED狀態
private static final int TERMINATED = 3 << COUNT_BITS;
// 附上英文的狀態解釋:
* 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
// 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; }
看一下線程池狀態變化的過程:
第三部分,提交任務到線程池:
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();
// 重複檢查線程池狀態,如果不是Running,就從任務隊列移除上面添加的任務,並執行拒絕
if (! isRunning(recheck) && remove(command))
reject(command);
// 如果工作線程數爲0,則嘗試新建一個任務爲null的線程
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
// 通過addWorker(command, false)新建一個線程,並將任務(command)添加到該線程中;
// 然後,啓動該線程從而執行任務。
// 如果addWorker(command, false)執行失敗,則通過reject()執行相應的拒絕策略的內容。
else if (!addWorker(command, false))
reject(command);
}
// 添加任務到線程上執行
private boolean addWorker(Runnable firstTask, boolean core) {
// 這段主要是判斷線程池的狀態,確定能夠添加任何到線程那執行
retry:
for (;;) {
// 獲取線程池的狀態和數量的int值
int c = ctl.get();
// 獲取線程池的狀態
int rs = runStateOf(c);
// Check if queue empty only if necessary.
// 有效性檢查:線程池關閉了,任務爲null,工作隊列爲空
if (rs >= SHUTDOWN &&
! (rs == SHUTDOWN &&
firstTask == null &&
! workQueue.isEmpty()))
return false;
// 死循環+cas保證線程安全
for (;;) {
// 獲取工作線程個數
int wc = workerCountOf(c);
// 如果工作線程數達到最大容量或者是核心線程數大小就返回false
if (wc >= CAPACITY ||
wc >= (core ? corePoolSize : maximumPoolSize))
return false;
// 工作線程數+1,失敗就跳出循環重試
if (compareAndIncrementWorkerCount(c))
break retry;
c = ctl.get(); // Re-read ctl
// 如果和之前的線程池狀態不一致了,就繼續從retry重新開始
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 {
// 新建一個工作線程,與任務綁定,等待run
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 {
// 如果線程沒有start起來,就把那個線程從集合移除,線程數量ctl - 1
if (! workerStarted)
addWorkerFailed(w);
}
return workerStarted;
}
// 工作線程類
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. */
// 工廠失敗時爲null
final Thread thread;
/** Initial task to run. Possibly null. */
// 需要運行的任務,可以爲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
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);
}
}
// submit是AbstractExecutorService的方法
// submit方法也是調用execut方法
public Future<?> submit(Runnable task) {
if (task == null) throw new NullPointerException();
RunnableFuture<Void> ftask = newTaskFor(task, null);
execute(ftask);
return ftask;
}
// 有返回值的submit
public <T> Future<T> submit(Runnable task, T result) {
if (task == null) throw new NullPointerException();
RunnableFuture<T> ftask = newTaskFor(task, result);
execute(ftask);
return ftask;
}
// 傳入Callable,也有返回值
public <T> Future<T> submit(Callable<T> task) {
if (task == null) throw new NullPointerException();
RunnableFuture<T> ftask = newTaskFor(task);
execute(ftask);
return ftask;
}
第四部分,關閉線程池:
// 關閉連接池,不會接受新的任務,但是會把已經提交的任務執行完畢
public void shutdown() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
// 安全檢查
checkShutdownAccess();
// 把線程池狀態置SHUTDOWN
advanceRunState(SHUTDOWN);
// 中斷空閒線程
interruptIdleWorkers();
onShutdown(); // hook for ScheduledThreadPoolExecutor
} finally {
mainLock.unlock();
}
tryTerminate();
}
// 中斷空閒線程
private void interruptIdleWorkers() {
interruptIdleWorkers(false);
}
private void interruptIdleWorkers(boolean onlyOne) {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
// 遍歷線程集合,如果不處於中斷狀態就中斷
for (Worker w : workers) {
Thread t = w.thread;
if (!t.isInterrupted() && w.tryLock()) {
try {
t.interrupt();
} catch (SecurityException ignore) {
} finally {
w.unlock();
}
}
if (onlyOne)
break;
}
} finally {
mainLock.unlock();
}
}
// 嘗試把狀態最後變成Terminated
final void tryTerminate() {
for (;;) {
int c = ctl.get();
// 判斷中斷的准入條件
// 正在運行的不行,處於TIDYING不行,處於SHUTDOWN 但任務隊列不爲空的不行
if (isRunning(c) ||
runStateAtLeast(c, TIDYING) ||
(runStateOf(c) == SHUTDOWN && ! workQueue.isEmpty()))
return;
// 如果工作線程不爲0,就中斷空閒線程
if (workerCountOf(c) != 0) { // Eligible to terminate
interruptIdleWorkers(ONLY_ONE);
return;
}
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
if (ctl.compareAndSet(c, ctlOf(TIDYING, 0))) {
try {
// 當線程終止了就調用這個方法
terminated();
} finally {
// 最後把ctl置0
ctl.set(ctlOf(TERMINATED, 0));
termination.signalAll();
}
return;
}
} finally {
mainLock.unlock();
}
// else retry on failed CAS
}
}
// 馬上關閉線程池,把狀態變STOP,終止正在執行的線程,返回等待執行的任務集合
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;
}
// 把任務隊列裏面的任務通過drainTo()放到一個list裏面,如果是延時的隊列,則可能失敗,需要
// 一個一個的刪除
private List<Runnable> drainQueue() {
BlockingQueue<Runnable> q = workQueue;
ArrayList<Runnable> taskList = new ArrayList<Runnable>();
q.drainTo(taskList);
if (!q.isEmpty()) {
for (Runnable r : q.toArray(new Runnable[0])) {
if (q.remove(r))
taskList.add(r);
}
}
return taskList;
}
第五部分,拒絕策略:
線程池產生拒絕的場景一般有兩個:一個是線程池異常關閉了,另一個是添加到線程池的任務數量已經超過阻塞隊裏最大值了
四種拒絕策略:
AbortPolicy -- 當任務添加到線程池中被拒絕時,它將拋出 RejectedExecutionException 異常。 默認的拒絕策略!
CallerRunsPolicy -- 當任務添加到線程池中被拒絕時,會在線程池當前正在運行的Thread線程池中處理被拒絕的任務。馬上執行。
DiscardOldestPolicy -- 當任務添加到線程池中被拒絕時,線程池會放棄等待隊列中最舊的未處理任務,然後將被拒絕的任務添加到等待隊列中。
DiscardPolicy -- 當任務添加到線程池中被拒絕時,線程池將丟棄被拒絕的任務。
使用方式:
// 構造函數指定
static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(3, 5, 2000, TimeUnit.MILLISECONDS, queue, new ThreadPoolExecutor.AbortPolicy());
// 線程池的set方法
threadPool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());