日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

Java線程池中線程異常后:是銷毀還是復用

來源: 責編: 時間:2024-06-14 08:52:26 132觀看
導讀一個線程池中的線程異常了,那么線程池會怎么處理這個線程?需要說明,本文的線程池都是java.util.concurrent.ExecutorService線程池,本文將圍繞驗證,閱讀源碼倆方面來解析這個問題。代碼驗證驗證execute提交線程池中測試代

Yiw28資訊網——每日最新資訊28at.com

一個線程池中的線程異常了,那么線程池會怎么處理這個線程?Yiw28資訊網——每日最新資訊28at.com

Yiw28資訊網——每日最新資訊28at.com

Yiw28資訊網——每日最新資訊28at.com

需要說明,本文的線程池都是java.util.concurrent.ExecutorService線程池,本文將圍繞驗證,閱讀源碼倆方面來解析這個問題。Yiw28資訊網——每日最新資訊28at.com

Yiw28資訊網——每日最新資訊28at.com

代碼驗證

驗證execute提交線程池中

測試代碼:Yiw28資訊網——每日最新資訊28at.com

public class ThreadPoolExecutorDeadTest {    public static void main(String[] args) throws InterruptedException {        ExecutorService executorService = buildThreadPoolExecutor();        executorService.execute(() -> exeTask("execute"));        executorService.execute(() -> exeTask("execute"));        executorService.execute(() -> exeTask("execute-exception"));        executorService.execute(() -> exeTask("execute"));        executorService.execute(() -> exeTask("execute"));        Thread.sleep(5000);        System.out.println("再次執行任務=======================");        executorService.execute(() -> exeTask("execute"));        executorService.execute(() -> exeTask("execute"));        executorService.execute(() -> exeTask("execute"));        executorService.execute(() -> exeTask("execute"));        executorService.execute(() -> exeTask("execute"));    }    public static ExecutorService buildThreadPoolExecutor() {        return new ThreadPoolExecutor(5, 10, 30, TimeUnit.SECONDS,                new LinkedBlockingQueue<>(1000), new ThreadFactoryBuilder().setNameFormat("test-%s").build()                , new ThreadPoolExecutor.CallerRunsPolicy());    }    private static void exeTask(String name) {        String printStr = "[thread-name:" + Thread.currentThread().getName() + ",執行方式:" + name + "]";        if ("execute-exception".equals(name)) {            throw new RuntimeException(printStr + ", 我拋異常了");        } else {            System.out.println(printStr);        }    }}

執行結果如下:Yiw28資訊網——每日最新資訊28at.com

圖片圖片Yiw28資訊網——每日最新資訊28at.com

結論:Yiw28資訊網——每日最新資訊28at.com

execute 提交到線程池的方式,如果執行中拋出異常,并且沒有在執行邏輯中catch,那么會拋出異常,并且移除拋出異常的線程,創建新的線程放入到線程池中。Yiw28資訊網——每日最新資訊28at.com

驗證submit提交線程池中

測試代碼:Yiw28資訊網——每日最新資訊28at.com

public class ThreadPoolExecutorDeadTest {    public static void main(String[] args) throws InterruptedException {        ExecutorService executorService = buildThreadPoolExecutor();        executorService.submit(() -> exeTask("execute"));        executorService.submit(() -> exeTask("execute"));        executorService.submit(() -> exeTask("execute-exception"));        executorService.submit(() -> exeTask("execute"));        executorService.submit(() -> exeTask("execute"));        Thread.sleep(5000);        System.out.println("再次執行任務=======================");        executorService.submit(() -> exeTask("execute"));        executorService.submit(() -> exeTask("execute"));        executorService.submit(() -> exeTask("execute"));        executorService.submit(() -> exeTask("execute"));        executorService.submit(() -> exeTask("execute"));    }    public static ExecutorService buildThreadPoolExecutor() {        return new ThreadPoolExecutor(5, 10, 30, TimeUnit.SECONDS,                new LinkedBlockingQueue<>(1000), new ThreadFactoryBuilder().setNameFormat("test-%s").build()                , new ThreadPoolExecutor.CallerRunsPolicy());    }    private static void exeTask(String name) {        String printStr = "[thread-name:" + Thread.currentThread().getName() + ",執行方式:" + name + "]";        if ("execute-exception".equals(name)) {            throw new RuntimeException(printStr + ", 我拋異常了");        } else {            System.out.println(printStr);        }    }}

執行結果如下:Yiw28資訊網——每日最新資訊28at.com

圖片圖片Yiw28資訊網——每日最新資訊28at.com

結論:Yiw28資訊網——每日最新資訊28at.com

submit 提交到線程池的方式,如果執行中拋出異常,并且沒有catch,不會拋出異常,不會創建新的線程。Yiw28資訊網——每日最新資訊28at.com

源碼解析

1.java.util.concurrent.AbstractExecutorService#submit(java.lang.Runnable);Yiw28資訊網——每日最新資訊28at.com

圖片圖片Yiw28資訊網——每日最新資訊28at.com

2. 查看execute方法的執行邏輯;Yiw28資訊網——每日最新資訊28at.com

Yiw28資訊網——每日最新資訊28at.com

圖片圖片Yiw28資訊網——每日最新資訊28at.com

3. java.util.concurrent.ThreadPoolExecutor#processWorkerExit;Yiw28資訊網——每日最新資訊28at.com

圖片圖片Yiw28資訊網——每日最新資訊28at.com

可以發現,如果拋出異常,會移除拋出異常的線程,創建新的線程。Yiw28資訊網——每日最新資訊28at.com

4. 為什么submit方法,沒有創建新的線程,而是繼續復用原線程;Yiw28資訊網——每日最新資訊28at.com

還記得,我們在3.1的時候,發現submit也是調用了execute方法,但是在調用之前,包裝了一層 RunnableFuture,那一定是在RunnableFuture的實現 FutureTask中有特殊處理了,我們查看源碼可以發現。Yiw28資訊網——每日最新資訊28at.com

圖片圖片Yiw28資訊網——每日最新資訊28at.com

圖片圖片Yiw28資訊網——每日最新資訊28at.com

圖片圖片Yiw28資訊網——每日最新資訊28at.com

圖片圖片Yiw28資訊網——每日最新資訊28at.com

但是,我們通過java.util.concurrent.FutureTask#get()就可以獲取對應的異常信息。Yiw28資訊網——每日最新資訊28at.com

Yiw28資訊網——每日最新資訊28at.com

總結

當一個線程池里面的線程異常后:Yiw28資訊網——每日最新資訊28at.com

  • 當執行方式是execute時,可以看到堆棧異常的輸出,線程池會把這個線程移除掉,并創建一個新的線程放到線程池中。
  • 當執行方式是submit時,堆棧異常沒有輸出。但是調用Future.get()方法時,可以捕獲到異常,不會把這個線程移除掉,也不會創建新的線程放入到線程池中。

以上倆種執行方式,都不會影響線程池里面其他線程的正常執行。Yiw28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-93697-0.htmlJava線程池中線程異常后:是銷毀還是復用

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 大廠面試必備:如何輕松實現分布式Session管理?

下一篇: Go必知必會:數組和切片詳解

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 甘孜| 马龙县| 平凉市| 景东| 靖远县| 孝义市| 和林格尔县| 庆阳市| 新乡县| 胶州市| 土默特右旗| 济源市| 望江县| 牟定县| 黑龙江省| 拉孜县| 辽阳市| 安平县| 大邑县| 陇南市| 莲花县| 耒阳市| 永善县| 马关县| 大邑县| 荔浦县| 古蔺县| 安吉县| 清涧县| 宝鸡市| 宾川县| 双流县| 鄂尔多斯市| 宜兰县| 张掖市| 扶余县| 江达县| 莱州市| 三江| 平度市| 渭源县|