平時開發(fā)中辅甥,大家更多的關(guān)注的是線程池的創(chuàng)建狡忙、任務(wù)的提交和執(zhí)行。往往會忽略線程池的關(guān)閉弓叛,甚至忘記調(diào)用shutdown()
方法彰居,導(dǎo)致內(nèi)存溢出。大多知道需要調(diào)用shutdown()關(guān)閉線程池撰筷,也少研究其真正的關(guān)閉過程陈惰。
首先看源碼中的一句注釋:
A pool that is no longer referenced in a program and has no remaining threads will be shutdown automatically.
如果程序中不再持有線程池的引用,并且線程池中沒有線程時闭专,線程池將會自動關(guān)閉奴潘。
線程池自動關(guān)閉的兩個條件:1、線程池的引用不可達(dá)影钉;2画髓、線程池中沒有線程;
這里對于條件2解釋一下平委,線程池中沒有線程是指線程池中的所有線程都已運(yùn)行完自動消亡奈虾。然而我們常用的FixedThreadPool的核心線程沒有超時策略,所以并不會自動關(guān)閉廉赔。
展示兩種不同線程池 不關(guān)閉 的情況:
1肉微、FixedThreadPool 示例
public static void main(String[] args) {
while(true) {
ExecutorService executorService = Executors.newFixedThreadPool(8);
executorService.execute(() -> System.out.println("running"));
executorService = null;
}
}
輸出結(jié)果:
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的核心線程不會自動超時關(guān)閉,使用時必須在適當(dāng)?shù)臅r候調(diào)用shutdown()方法蜡塌。
2碉纳、 CachedThreadPool 示例
public static void main(String[] args) {
while(true) {
// 默認(rèn)keepAliveTime為 60s
ExecutorService executorService = Executors.newCachedThreadPool();
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executorService;
// 為了更好的模擬,動態(tài)修改為1納秒
threadPoolExecutor.setKeepAliveTime(1, TimeUnit.NANOSECONDS);
threadPoolExecutor.execute(() -> System.out.println("running"));
}
}
輸出結(jié)果:
running
running
running
running
running
......
CachedThreadPool 的線程 keepAliveTime 默認(rèn)為 60s 馏艾,核心線程數(shù)量為 0 劳曹,所以不會有核心線程存活阻止線程池自動關(guān)閉。 詳見 線程池之ThreadPoolExecutor構(gòu)造 琅摩,為了更快的模擬铁孵,構(gòu)造后將 keepAliveTime 修改為1納秒,相當(dāng)于線程執(zhí)行完馬上會消亡房资,所以線程池可以被回收蜕劝。實際開發(fā)中,如果CachedThreadPool 確實忘記關(guān)閉,在一定時間后是可以被回收的岖沛。但仍然建議顯示關(guān)閉暑始。
然而,線程池關(guān)閉的意義不僅僅在于結(jié)束線程執(zhí)行婴削,避免內(nèi)存溢出蒋荚,因為大多使用的場景并非上述示例那樣 朝生夕死。線程池一般是持續(xù)工作的全局場景馆蠕,如數(shù)據(jù)庫連接池。
本文更多要討論的是當(dāng)線程池調(diào)用shutdown方法后惊奇,會經(jīng)歷些什么互躬?思考一下幾個問題:
- 是否可以繼續(xù)接受新任務(wù)?繼續(xù)提交新任務(wù)會怎樣颂郎?
- 等待隊列里的任務(wù)是否還會執(zhí)行吼渡?
- 正在執(zhí)行的任務(wù)是否會立即中斷?
問題1:是否可以繼續(xù)接受新任務(wù)乓序?繼續(xù)提交新任務(wù)會怎樣寺酪?
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"));
}
輸出結(jié)果如下:
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)
當(dāng)線程池關(guān)閉后,繼續(xù)提交新任務(wù)會拋出異常替劈。這句話也不夠準(zhǔn)確寄雀,不一定是拋出異常,而是執(zhí)行拒絕策略陨献,默認(rèn)的拒絕策略是拋出異常盒犹。可參見 線程池之ThreadPoolExecutor構(gòu)造 里面自定義線程池的例子眨业,自定義了忽略策略急膀,但被拒絕時并沒有拋出異常。
問題2:等待隊列里的任務(wù)是否還會執(zhí)行龄捡?
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構(gòu)造了一個線程池,在線程池啟動前聘殖,我們先將工作隊列填充100個任務(wù)晨雳,然后執(zhí)行task 0
后立即shutdown()
線程池,來驗證線程池關(guān)閉隊列的任務(wù)運(yùn)行狀態(tài)就斤。
輸出結(jié)果如下:
......
task 0 is running
task 0 is over
workQueue size = 100 after shutdown //表示線程池關(guān)閉后悍募,隊列任然有100個任務(wù)
task 1 is running
......
task 100 is running
task 100 is over
從結(jié)果中我們可以看到,線程池雖然關(guān)閉洋机,但是隊列中的任務(wù)任然繼續(xù)執(zhí)行坠宴,所以用 shutdown()
方式關(guān)閉線程池時需要考慮是否是你想要的效果。
如果你希望線程池中的等待隊列中的任務(wù)不繼續(xù)執(zhí)行绷旗,可以使用shutdownNow()
方法喜鼓,將上述代碼進(jìn)行調(diào)整副砍,如下:
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有返回值,返回被拋棄的任務(wù)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");
}
}
}
輸出結(jié)果如下:
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
從上述輸出可以看到庄岖,只有任務(wù)0執(zhí)行完畢豁翎,其他任務(wù)都被drop掉了,dropList的size為100隅忿。通過dropList我們可以對未處理的任務(wù)進(jìn)行進(jìn)一步的處理心剥,如log記錄,轉(zhuǎn)發(fā)等背桐;
問題3:正在執(zhí)行的任務(wù)是否會立即中斷优烧?
要驗證這個問題,需要對線程的 interrupt 方法有一定了解链峭。
推薦閱讀 ——線程中斷機(jī)制
關(guān)于 interrupt 方法:
首先畦娄,一個線程不應(yīng)該由其他線程來強(qiáng)制中斷或停止,而是應(yīng)該由線程自己自行停止弊仪。
所以熙卡,Thread.stop, Thread.suspend, Thread.resume 都已經(jīng)被廢棄了。
而 Thread.interrupt 的作用其實也不是中斷線程励饵,而是「通知線程應(yīng)該中斷了」驳癌,具體到底中斷還是繼續(xù)運(yùn)行,應(yīng)該由被通知的線程自己處理役听。
具體來說喂柒,當(dāng)對一個線程,調(diào)用 interrupt() 時禾嫉,
① 如果線程處于被阻塞狀態(tài)(例如處于sleep, wait, join 等狀態(tài))灾杰,那么線程將立即退出被阻塞狀態(tài),并拋出一個InterruptedException異常熙参。僅此而已艳吠。
② 如果線程處于正常活動狀態(tài)孽椰,那么會將該線程的中斷標(biāo)志設(shè)置為 true昭娩,僅此而已。被設(shè)置中斷標(biāo)志的線程將繼續(xù)正常運(yùn)行黍匾,不受影響栏渺。
interrupt() 并不能真正的中斷線程,需要被調(diào)用的線程自己進(jìn)行配合才行锐涯。也就是說磕诊,一個線程如果有被中斷的需求,那么就可以這樣做。
① 在正常運(yùn)行任務(wù)時霎终,經(jīng)常檢查本線程的中斷標(biāo)志位滞磺,如果被設(shè)置了中斷標(biāo)志就自行停止線程。
② 在調(diào)用阻塞方法時正確處理InterruptedException異常莱褒。(例如击困,catch異常后就結(jié)束線程。)
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);
}
}
}
}
輸出結(jié)果如下:
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
為了體現(xiàn)在任務(wù)執(zhí)行中打斷广凸,在主線程進(jìn)行短暫 sleep 阅茶, task 中 調(diào)用 Thread.yield() ,出讓時間片谅海。從結(jié)果中可以看到目派,線程池被關(guān)閉后,正則運(yùn)行的任務(wù)沒有被 interrupt胁赢。說明shutdown()
方法不會 interrupt 運(yùn)行中線程。再將其改修改為shutdownNow()
后輸出結(jié)果如下:
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任務(wù)沒有執(zhí)行完智末,執(zhí)行到中間的時候就被 interrupt 后沒有繼續(xù)執(zhí)行了。
總結(jié)徒河,想要正確的關(guān)閉線程池系馆,并不是簡單的調(diào)用shutdown方法那么簡單,要考慮到應(yīng)用場景的需求顽照,如何拒絕新來的請求任務(wù)由蘑?如何處理等待隊列中的任務(wù)?如何處理正在執(zhí)行的任務(wù)代兵?想好這幾個問題尼酿,在確定如何優(yōu)雅而正確的關(guān)閉線程池。
PS:線程被 interrupt 后植影,需要再run方法中單獨(dú)處理 interrupted 狀態(tài)裳擎,interrupt 更類似一個標(biāo)志位,不會直接打斷線程的執(zhí)行思币。
多線程系列目錄(不斷更新中):
線程啟動原理
線程中斷機(jī)制
多線程實現(xiàn)方式
FutureTask實現(xiàn)原理
線程池之ThreadPoolExecutor概述
線程池之ThreadPoolExecutor使用
線程池之ThreadPoolExecutor狀態(tài)控制
線程池之ThreadPoolExecutor執(zhí)行原理
線程池之ScheduledThreadPoolExecutor概述
線程池之ScheduledThreadPoolExecutor調(diào)度原理
線程池的優(yōu)雅關(guān)閉實踐