平時開發中,大家更多的關注的是線程池的創建、任務的提交和執行。往往會忽略線程池的關閉,甚至忘記調用shutdown()
方法,導致內存溢出。大多知道需要調用shutdown()關閉線程池,也少研究其真正的關閉過程。
首先看源碼中的一句注釋:
A pool that is no longer referenced in a program and has no remaining threads will be shutdown automatically.
如果程序中不再持有線程池的引用,并且線程池中沒有線程時,線程池將會自動關閉。
線程池自動關閉的兩個條件:1、線程池的引用不可達;2、線程池中沒有線程;
這里對于條件2解釋一下,線程池中沒有線程是指線程池中的所有線程都已運行完自動消亡。然而我們常用的FixedThreadPool的核心線程沒有超時策略,所以并不會自動關閉。
展示兩種不同線程池 不關閉 的情況:
1、FixedThreadPool 示例
public static void main(String[] args) {
while(true) {
ExecutorService executorService = Executors.newFixedThreadPool(8);
executorService.execute(() -> System.out.println("running"));
executorService = null;
}
}
輸出結果:
running
......
running
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:714)
at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:950)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1357)
at test.PoolTest.main(PoolTest.java:29)
因為FixedThreadPool的核心線程不會自動超時關閉,使用時必須在適當的時候調用shutdown()方法。
2、 CachedThreadPool 示例
public static void main(String[] args) {
while(true) {
// 默認keepAliveTime為 60s
ExecutorService executorService = Executors.newCachedThreadPool();
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executorService;
// 為了更好的模擬,動態修改為1納秒
threadPoolExecutor.setKeepAliveTime(1, TimeUnit.NANOSECONDS);
threadPoolExecutor.execute(() -> System.out.println("running"));
}
}
輸出結果:
running
running
running
running
running
......
CachedThreadPool 的線程 keepAliveTime 默認為 60s ,核心線程數量為 0 ,所以不會有核心線程存活阻止線程池自動關閉。 詳見 線程池之ThreadPoolExecutor構造 ,為了更快的模擬,構造后將 keepAliveTime 修改為1納秒,相當于線程執行完馬上會消亡,所以線程池可以被回收。實際開發中,如果CachedThreadPool 確實忘記關閉,在一定時間后是可以被回收的。但仍然建議顯示關閉。
然而,線程池關閉的意義不僅僅在于結束線程執行,避免內存溢出,因為大多使用的場景并非上述示例那樣 朝生夕死。線程池一般是持續工作的全局場景,如數據庫連接池。
本文更多要討論的是當線程池調用shutdown方法后,會經歷些什么?思考一下幾個問題:
- 是否可以繼續接受新任務?繼續提交新任務會怎樣?
- 等待隊列里的任務是否還會執行?
- 正在執行的任務是否會立即中斷?
問題1:是否可以繼續接受新任務?繼續提交新任務會怎樣?
public static void main(String[] args) {
ThreadPoolExecutor executor = new ThreadPoolExecutor(4, 4, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
executor.execute(() -> System.out.println("before shutdown"));
executor.shutdown();
executor.execute(() -> System.out.println("after shutdown"));
}
輸出結果如下:
before shutdown
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task PoolTest$$Lambda$2/142257191@3e3abc88 rejected from java.util.concurrent.ThreadPoolExecutor@6ce253f1[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 1]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
at PoolTest.main(PoolTest.java:12)
當線程池關閉后,繼續提交新任務會拋出異常。這句話也不夠準確,不一定是拋出異常,而是執行拒絕策略,默認的拒絕策略是拋出異常。可參見 線程池之ThreadPoolExecutor構造 里面自定義線程池的例子,自定義了忽略策略,但被拒絕時并沒有拋出異常。
問題2:等待隊列里的任務是否還會執行?
public class WaitqueueTest {
public static void main(String[] args) {
BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();
for(int i = 1; i <= 100 ; i++){
workQueue.add(new Task(String.valueOf(i)));
}
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 10, TimeUnit.SECONDS, workQueue);
executor.execute(new Task("0"));
executor.shutdown();
System.out.println("workQueue size = " + workQueue.size() + " after shutdown");
}
static class Task implements Runnable{
String name;
public Task(String name) {
this.name = name;
}
@Override
public void run() {
for(int i = 1; i <= 10; i++){
System.out.println("task " + name + " is running");
}
System.out.println("task " + name + " is over");
}
}
}
這個demo解釋一下,我們用LinkedBlockingQueue構造了一個線程池,在線程池啟動前,我們先將工作隊列填充100個任務,然后執行task 0
后立即shutdown()
線程池,來驗證線程池關閉隊列的任務運行狀態。
輸出結果如下:
......
task 0 is running
task 0 is over
workQueue size = 100 after shutdown //表示線程池關閉后,隊列任然有100個任務
task 1 is running
......
task 100 is running
task 100 is over
從結果中我們可以看到,線程池雖然關閉,但是隊列中的任務任然繼續執行,所以用 shutdown()
方式關閉線程池時需要考慮是否是你想要的效果。
如果你希望線程池中的等待隊列中的任務不繼續執行,可以使用shutdownNow()
方法,將上述代碼進行調整,如下:
public class WaitqueueTest {
public static void main(String[] args) {
BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();
for(int i = 1; i <= 100 ; i++){
workQueue.add(new Task(String.valueOf(i)));
}
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 10, TimeUnit.SECONDS, workQueue);
executor.execute(new Task("0"));
// shutdownNow有返回值,返回被拋棄的任務list
List<Runnable> dropList = executor.shutdownNow();
System.out.println("workQueue size = " + workQueue.size() + " after shutdown");
System.out.println("dropList size = " + dropList.size());
}
static class Task implements Runnable{
String name;
public Task(String name) {
this.name = name;
}
@Override
public void run() {
for(int i = 1; i <= 10; i++){
System.out.println("task " + name + " is running");
}
System.out.println("task " + name + " is over");
}
}
}
輸出結果如下:
task 0 is running
workQueue size = 0 after shutdown
task 0 is running
task 0 is running
task 0 is running
task 0 is running
task 0 is running
task 0 is running
task 0 is running
task 0 is running
task 0 is running
dropList size = 100
task 0 is over
從上述輸出可以看到,只有任務0執行完畢,其他任務都被drop掉了,dropList的size為100。通過dropList我們可以對未處理的任務進行進一步的處理,如log記錄,轉發等;
問題3:正在執行的任務是否會立即中斷?
要驗證這個問題,需要對線程的 interrupt 方法有一定了解。
推薦閱讀 ——線程中斷機制
關于 interrupt 方法:
首先,一個線程不應該由其他線程來強制中斷或停止,而是應該由線程自己自行停止。
所以,Thread.stop, Thread.suspend, Thread.resume 都已經被廢棄了。
而 Thread.interrupt 的作用其實也不是中斷線程,而是「通知線程應該中斷了」,具體到底中斷還是繼續運行,應該由被通知的線程自己處理。
具體來說,當對一個線程,調用 interrupt() 時,
① 如果線程處于被阻塞狀態(例如處于sleep, wait, join 等狀態),那么線程將立即退出被阻塞狀態,并拋出一個InterruptedException異常。僅此而已。
② 如果線程處于正常活動狀態,那么會將該線程的中斷標志設置為 true,僅此而已。被設置中斷標志的線程將繼續正常運行,不受影響。
interrupt() 并不能真正的中斷線程,需要被調用的線程自己進行配合才行。也就是說,一個線程如果有被中斷的需求,那么就可以這樣做。
① 在正常運行任務時,經常檢查本線程的中斷標志位,如果被設置了中斷標志就自行停止線程。
② 在調用阻塞方法時正確處理InterruptedException異常。(例如,catch異常后就結束線程。)
public class InteruptTest {
public static void main(String[] args) throws InterruptedException {
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
executor.execute(new Task("0"));
Thread.sleep(1);
executor.shutdown();
System.out.println("executor has been shutdown");
}
static class Task implements Runnable {
String name;
public Task(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 1; i <= 100 && !Thread.interrupted(); i++) {
Thread.yield();
System.out.println("task " + name + " is running, round " + i);
}
}
}
}
輸出結果如下:
task 0 is running, round 1
task 0 is running, round 2
task 0 is running, round 3
......
task 0 is running, round 28
executor has been shutdown
......
task 0 is running, round 99
task 0 is running, round 100
為了體現在任務執行中打斷,在主線程進行短暫 sleep , task 中 調用 Thread.yield() ,出讓時間片。從結果中可以看到,線程池被關閉后,正則運行的任務沒有被 interrupt。說明shutdown()
方法不會 interrupt 運行中線程。再將其改修改為shutdownNow()
后輸出結果如下:
task 0 is running, round 1
task 0 is running, round 2
......
task 0 is running, round 56
task 0 is running, round 57
task 0 is running, round 58
task 0 is running, round 59
executor has been shutdown
修改為shutdownNow()
后,task任務沒有執行完,執行到中間的時候就被 interrupt 后沒有繼續執行了。
總結,想要正確的關閉線程池,并不是簡單的調用shutdown方法那么簡單,要考慮到應用場景的需求,如何拒絕新來的請求任務?如何處理等待隊列中的任務?如何處理正在執行的任務?想好這幾個問題,在確定如何優雅而正確的關閉線程池。
PS:線程被 interrupt 后,需要再run方法中單獨處理 interrupted 狀態,interrupt 更類似一個標志位,不會直接打斷線程的執行。
多線程系列目錄(不斷更新中):
線程啟動原理
線程中斷機制
多線程實現方式
FutureTask實現原理
線程池之ThreadPoolExecutor概述
線程池之ThreadPoolExecutor使用
線程池之ThreadPoolExecutor狀態控制
線程池之ThreadPoolExecutor執行原理
線程池之ScheduledThreadPoolExecutor概述
線程池之ScheduledThreadPoolExecutor調度原理
線程池的優雅關閉實踐