線程池的優(yōu)雅關(guān)閉實(shí)踐

平時(shí)開(kāi)發(fā)中,大家更多的關(guān)注的是線程池的創(chuàng)建、任務(wù)的提交和執(zhí)行。往往會(huì)忽略線程池的關(guān)閉,甚至忘記調(diào)用shutdown()方法,導(dǎo)致內(nèi)存溢出。大多知道需要調(diào)用shutdown()關(guān)閉線程池,也少研究其真正的關(guān)閉過(guò)程。

首先看源碼中的一句注釋:

A pool that is no longer referenced in a program and has no remaining threads will be shutdown automatically.
如果程序中不再持有線程池的引用,并且線程池中沒(méi)有線程時(shí),線程池將會(huì)自動(dòng)關(guān)閉。

線程池自動(dòng)關(guān)閉的兩個(gè)條件:1、線程池的引用不可達(dá);2、線程池中沒(méi)有線程;

這里對(duì)于條件2解釋一下,線程池中沒(méi)有線程是指線程池中的所有線程都已運(yùn)行完自動(dòng)消亡。然而我們常用的FixedThreadPool的核心線程沒(méi)有超時(shí)策略,所以并不會(huì)自動(dòng)關(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)

因?yàn)镕ixedThreadPool的核心線程不會(huì)自動(dòng)超時(shí)關(guān)閉,使用時(shí)必須在適當(dāng)?shù)臅r(shí)候調(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;
        // 為了更好的模擬,動(dòng)態(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 ,所以不會(huì)有核心線程存活阻止線程池自動(dòng)關(guān)閉。 詳見(jiàn) 線程池之ThreadPoolExecutor構(gòu)造 ,為了更快的模擬,構(gòu)造后將 keepAliveTime 修改為1納秒,相當(dāng)于線程執(zhí)行完馬上會(huì)消亡,所以線程池可以被回收。實(shí)際開(kāi)發(fā)中,如果CachedThreadPool 確實(shí)忘記關(guān)閉,在一定時(shí)間后是可以被回收的。但仍然建議顯示關(guān)閉。

然而,線程池關(guān)閉的意義不僅僅在于結(jié)束線程執(zhí)行,避免內(nèi)存溢出,因?yàn)榇蠖嗍褂玫膱?chǎng)景并非上述示例那樣 朝生夕死。線程池一般是持續(xù)工作的全局場(chǎng)景,如數(shù)據(jù)庫(kù)連接池。

本文更多要討論的是當(dāng)線程池調(diào)用shutdown方法后,會(huì)經(jīng)歷些什么?思考一下幾個(gè)問(wèn)題:

  1. 是否可以繼續(xù)接受新任務(wù)?繼續(xù)提交新任務(wù)會(huì)怎樣?
  2. 等待隊(duì)列里的任務(wù)是否還會(huì)執(zhí)行?
  3. 正在執(zhí)行的任務(wù)是否會(huì)立即中斷?

問(wèn)題1:是否可以繼續(xù)接受新任務(wù)?繼續(xù)提交新任務(wù)會(huì)怎樣?

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ù)會(huì)拋出異常。這句話也不夠準(zhǔn)確,不一定是拋出異常,而是執(zhí)行拒絕策略,默認(rèn)的拒絕策略是拋出異常??蓞⒁?jiàn) 線程池之ThreadPoolExecutor構(gòu)造 里面自定義線程池的例子,自定義了忽略策略,但被拒絕時(shí)并沒(méi)有拋出異常。

問(wèn)題2:等待隊(duì)列里的任務(wù)是否還會(huì)執(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");
        }
    }
}

這個(gè)demo解釋一下,我們用LinkedBlockingQueue構(gòu)造了一個(gè)線程池,在線程池啟動(dòng)前,我們先將工作隊(duì)列填充100個(gè)任務(wù),然后執(zhí)行task 0 后立即shutdown()線程池,來(lái)驗(yàn)證線程池關(guān)閉隊(duì)列的任務(wù)運(yùn)行狀態(tài)。
輸出結(jié)果如下:

......
task 0 is running
task 0 is over
workQueue size = 100 after shutdown //表示線程池關(guān)閉后,隊(duì)列任然有100個(gè)任務(wù)
task 1 is running
......
task 100 is running
task 100 is over

從結(jié)果中我們可以看到,線程池雖然關(guān)閉,但是隊(duì)列中的任務(wù)任然繼續(xù)執(zhí)行,所以用 shutdown()方式關(guān)閉線程池時(shí)需要考慮是否是你想要的效果。

如果你希望線程池中的等待隊(duì)列中的任務(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。通過(guò)dropList我們可以對(duì)未處理的任務(wù)進(jìn)行進(jìn)一步的處理,如log記錄,轉(zhuǎn)發(fā)等;

問(wèn)題3:正在執(zhí)行的任務(wù)是否會(huì)立即中斷?

要驗(yàn)證這個(gè)問(wèn)題,需要對(duì)線程的 interrupt 方法有一定了解。


推薦閱讀 ——線程中斷機(jī)制
關(guān)于 interrupt 方法:
首先,一個(gè)線程不應(yīng)該由其他線程來(lái)強(qiáng)制中斷或停止,而是應(yīng)該由線程自己自行停止。
所以,Thread.stop, Thread.suspend, Thread.resume 都已經(jīng)被廢棄了。
而 Thread.interrupt 的作用其實(shí)也不是中斷線程,而是「通知線程應(yīng)該中斷了」,具體到底中斷還是繼續(xù)運(yùn)行,應(yīng)該由被通知的線程自己處理。
具體來(lái)說(shuō),當(dāng)對(duì)一個(gè)線程,調(diào)用 interrupt() 時(shí),
① 如果線程處于被阻塞狀態(tài)(例如處于sleep, wait, join 等狀態(tài)),那么線程將立即退出被阻塞狀態(tài),并拋出一個(gè)InterruptedException異常。僅此而已。
② 如果線程處于正?;顒?dòng)狀態(tài),那么會(huì)將該線程的中斷標(biāo)志設(shè)置為 true,僅此而已。被設(shè)置中斷標(biāo)志的線程將繼續(xù)正常運(yùn)行,不受影響。
interrupt() 并不能真正的中斷線程,需要被調(diào)用的線程自己進(jìn)行配合才行。也就是說(shuō),一個(gè)線程如果有被中斷的需求,那么就可以這樣做。
① 在正常運(yùn)行任務(wù)時(shí),經(jīng)常檢查本線程的中斷標(biāo)志位,如果被設(shè)置了中斷標(biāo)志就自行停止線程。
② 在調(diào)用阻塞方法時(shí)正確處理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() ,出讓時(shí)間片。從結(jié)果中可以看到,線程池被關(guān)閉后,正則運(yùn)行的任務(wù)沒(méi)有被 interrupt。說(shuō)明shutdown()方法不會(huì) 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ù)沒(méi)有執(zhí)行完,執(zhí)行到中間的時(shí)候就被 interrupt 后沒(méi)有繼續(xù)執(zhí)行了。

總結(jié),想要正確的關(guān)閉線程池,并不是簡(jiǎn)單的調(diào)用shutdown方法那么簡(jiǎn)單,要考慮到應(yīng)用場(chǎng)景的需求,如何拒絕新來(lái)的請(qǐng)求任務(wù)?如何處理等待隊(duì)列中的任務(wù)?如何處理正在執(zhí)行的任務(wù)?想好這幾個(gè)問(wèn)題,在確定如何優(yōu)雅而正確的關(guān)閉線程池。

PS:線程被 interrupt 后,需要再run方法中單獨(dú)處理 interrupted 狀態(tài),interrupt 更類似一個(gè)標(biāo)志位,不會(huì)直接打斷線程的執(zhí)行。

多線程系列目錄(不斷更新中):
線程啟動(dòng)原理
線程中斷機(jī)制
多線程實(shí)現(xiàn)方式
FutureTask實(shí)現(xiàn)原理
線程池之ThreadPoolExecutor概述
線程池之ThreadPoolExecutor使用
線程池之ThreadPoolExecutor狀態(tài)控制
線程池之ThreadPoolExecutor執(zhí)行原理
線程池之ScheduledThreadPoolExecutor概述
線程池之ScheduledThreadPoolExecutor調(diào)度原理
線程池的優(yōu)雅關(guān)閉實(shí)踐

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。