一、創建一個線程池對象
使用Executors創建一個線程池常用的方法如下:
1、newFixedThreadPool()
說明:初始化一個指定線程數的線程池,其中 corePoolSize == maxiPoolSize,使用 LinkedBlockingQuene 作爲阻塞隊列
特點:即使當線程池沒有可執行任務時,也不會釋放線程。
2、newCachedThreadPool()
說明:初始化一個可以緩存線程的線程池,默認緩存60s,線程池的線程數可達到 Integer.MAX_VALUE,即 2147483647,內部使用 SynchronousQueue 作爲阻塞隊列;
特點:在沒有任務執行時,當線程的空閒時間超過 keepAliveTime,會自動釋放線程資源;當提交新任務時,如果沒有空閒線程,則創建新線程執行任務,會導致一定的系統開銷; 因此,使用時要注意控制併發的任務數,防止因創建大量的線程導致而降低性能。
3、newSingleThreadExecutor()
說明:初始化只有一個線程的線程池,內部使用 LinkedBlockingQueue 作爲阻塞隊列。
特點:如果該線程異常結束,會重新創建一個新的線程繼續執行任務,唯一的線程可以保證所提交任務的順序執行
4、newScheduledThreadPool()
特點:初始化的線程池可以在指定的時間內週期性的執行所提交的任務,在實際的業務場景中可以使用該線程池定期的同步數據。
線程池的構造方法如下:
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;
}
默認的線程工廠,Executors類的DefaultThreadFactory方法:
/**
* Thread factory capturing access control context and class loader
*/
static class PrivilegedThreadFactory extends DefaultThreadFactory {
private final AccessControlContext acc;
private final ClassLoader ccl;
PrivilegedThreadFactory() {
super();
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
// Calls to getContextClassLoader from this class
// never trigger a security check, but we check
// whether our callers have this permission anyways.
sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
// Fail fast
sm.checkPermission(new RuntimePermission("setContextClassLoader"));
}
this.acc = AccessController.getContext();
this.ccl = Thread.currentThread().getContextClassLoader();
}
public Thread newThread(final Runnable r) {
return super.newThread(new Runnable() {
public void run() {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
Thread.currentThread().setContextClassLoader(ccl);
r.run();
return null;
}
}, acc);
}
});
}
}
默認的拒絕策略
/**
* The default rejected execution handler
*/
private static final RejectedExecutionHandler defaultHandler =
new AbortPolicy();
/**
* A handler for rejected tasks that throws a
* {@code RejectedExecutionException}.
*/
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());
}
}
二、添加任務到線程池execute()
/**
* 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}.
*
* @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.
*/
// 獲取ctl對應的int值。該int值保存了"線程池中任務的數量"和"線程池狀態"信息
int c = ctl.get();
// 當線程池中的任務數量 < "核心池大小"時,即線程池中少於corePoolSize個任務。
// 則通過addWorker(command,true)新建一個線程,並將任務(command)添加到該線程中;然後,啓動該線程從而執行任務。
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
// 當線程池中的任務數量 >= "核心池大小"時,
// 而且,"線程池處於允許狀態"時,則嘗試將任務添加到阻塞隊列中。
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
// 再次確認“線程池狀態”,若線程池異常終止了,則刪除任務;然後通過reject()執行相應的拒絕策略的內容。
if (! isRunning(recheck) && remove(command))
reject(command);
// 否則,如果"線程池中任務數量"爲0,則通過addWorker(null,false)嘗試新建一個線程,新建線程對應的任務爲null。
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
// 通過addWorker(command,false)新建一個線程,並將任務(command)添加到該線程中;然後,啓動該線程從而執行任務。
// 如果addWorker(command, false)執行失敗,則通過reject()執行相應的拒絕策略的內
else if (!addWorker(command, false))
reject(command);
}
可以看到execute方法流程如下:(可參照英文註釋)
- 線程池中的任務數量小於核心池大小時,新建線程;
- 線程池中的任務數量大於等於核心池大小時,添加到任務隊列中;
- 若不能將任務添加到隊列中,則嘗試新建一個線程,失敗的話執行拒絕策略
下面來看addWorker方法:
private boolean addWorker(Runnable firstTask, boolean core) {
retry:
// 這部分是對狀態進行檢查,更新ctl的值
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 {
final ReentrantLock mainLock = this.mainLock;
// 新建Worker,並且指定firstTask爲Worker的第一個任務
w = new Worker(firstTask);
final Thread t = w.thread;
if (t != null) {
mainLock.lock();
try {
// Recheck while holding lock.
// Back out on ThreadFactory failure or if
// shut down before lock acquired.
int c = ctl.get();
int rs = runStateOf(c);
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;
}
三、關閉線程池
調用線程池的shutdown方法可以關閉線程池:
/**
* Initiates an orderly shutdown in which previously submitted
* tasks are executed, but no new tasks will be accepted.
* Invocation has no additional effect if already shut down.
*
* <p>This method does not wait for previously submitted tasks to
* complete execution. Use {@link #awaitTermination awaitTermination}
* to do that.
*
* @throws SecurityException {@inheritDoc}
*/
public void shutdown() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
// 檢查終止線程池的“線程”是否有權限
checkShutdownAccess();
// 設置線程池的狀態爲關閉狀態
advanceRunState(SHUTDOWN);
// 中斷線程池中空閒的線程
interruptIdleWorkers();
onShutdown(); // hook for ScheduledThreadPoolExecutor
} finally {
mainLock.unlock();
}
tryTerminate();
}
四、線程池狀態
線程池使用了原子類的Integer的對象來標識線程池的狀態:
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;
可以看到線程池公有以下五種狀態:
-
RUNNING:線程池處在RUNNING狀態時,能夠接收新任務,以及對已添加的任務進行處理。線程池的初始化狀態是RUNNING。
-
SHUTDOWN:調用線程池的shutdown()接口時,線程池由RUNNING轉到SHUTDOWN。線程池處在SHUTDOWN狀態時,不接收新任務,但能處理已添加的任務。
-
STOP:調用線程池的shutdownNow()接口時,線程池由(RUNNING or SHUTDOWN ) -> STOP。線程池處在STOP狀態時,不接收新任務,不處理已添加的任務,並且會中斷正在處理的任務。
-
TIDYING:當線程池在SHUTDOWN狀態下,阻塞隊列爲空並且線程池中執行的任務也爲空時,就會由 SHUTDOWN -> TIDYING。當線程池在STOP狀態下,線程池中執行的任務爲空時,就會由STOP -> TIDYING。當所有的任務已終止,ctl記錄的"任務數量"爲0,線程池會變爲TIDYING狀態。當線程池變爲TIDYING狀態時,會執行鉤子函數terminated()。terminated()在ThreadPoolExecutor類中是空的,若用戶想在線程池變爲TIDYING時,進行相應的處理;可以通過重載terminated()函數來實現。:
-
TERMINATED:線程池處在TIDYING狀態時,執行完terminated()之後,就會由TIDYING->TERMINATED。線程池徹底終止,就變成TERMINATED狀態。
五、線程池拒絕策略
線程池共包括4種拒絕策略:
- AbortPolicy:當任務添加到線程池中被拒絕時,它將拋出 RejectedExecutionException 異常。
- CallerRunsPolicy:當任務添加到線程池中被拒絕時,會在線程池當前正在運行的Thread線程池中處理被拒絕的任務。
- DiscardOldestPolicy:當任務添加到線程池中被拒絕時,線程池會放棄等待隊列中最舊的未處理任務,然後將被拒絕的任務添加到等待隊列中。
- DiscardPolicy:當任務添加到線程池中被拒絕時,線程池將丟棄被拒絕的任務。