AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

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

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

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

sld28資訊網(wǎng)——每日最新資訊28at.com

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

sld28資訊網(wǎng)——每日最新資訊28at.com

sld28資訊網(wǎng)——每日最新資訊28at.com

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

sld28資訊網(wǎng)——每日最新資訊28at.com

代碼驗證

驗證execute提交線程池中

測試代碼:sld28資訊網(wǎng)——每日最新資訊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("再次執(zhí)行任務=======================");        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() + ",執(zhí)行方式:" + name + "]";        if ("execute-exception".equals(name)) {            throw new RuntimeException(printStr + ", 我拋異常了");        } else {            System.out.println(printStr);        }    }}

執(zhí)行結果如下:sld28資訊網(wǎng)——每日最新資訊28at.com

圖片圖片sld28資訊網(wǎng)——每日最新資訊28at.com

結論:sld28資訊網(wǎng)——每日最新資訊28at.com

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

驗證submit提交線程池中

測試代碼:sld28資訊網(wǎng)——每日最新資訊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("再次執(zhí)行任務=======================");        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() + ",執(zhí)行方式:" + name + "]";        if ("execute-exception".equals(name)) {            throw new RuntimeException(printStr + ", 我拋異常了");        } else {            System.out.println(printStr);        }    }}

執(zhí)行結果如下:sld28資訊網(wǎng)——每日最新資訊28at.com

圖片圖片sld28資訊網(wǎng)——每日最新資訊28at.com

結論:sld28資訊網(wǎng)——每日最新資訊28at.com

submit 提交到線程池的方式,如果執(zhí)行中拋出異常,并且沒有catch,不會拋出異常,不會創(chuàng)建新的線程。sld28資訊網(wǎng)——每日最新資訊28at.com

源碼解析

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

圖片圖片sld28資訊網(wǎng)——每日最新資訊28at.com

2. 查看execute方法的執(zhí)行邏輯;sld28資訊網(wǎng)——每日最新資訊28at.com

sld28資訊網(wǎng)——每日最新資訊28at.com

圖片圖片sld28資訊網(wǎng)——每日最新資訊28at.com

3. java.util.concurrent.ThreadPoolExecutor#processWorkerExit;sld28資訊網(wǎng)——每日最新資訊28at.com

圖片圖片sld28資訊網(wǎng)——每日最新資訊28at.com

可以發(fā)現(xiàn),如果拋出異常,會移除拋出異常的線程,創(chuàng)建新的線程。sld28資訊網(wǎng)——每日最新資訊28at.com

4. 為什么submit方法,沒有創(chuàng)建新的線程,而是繼續(xù)復用原線程;sld28資訊網(wǎng)——每日最新資訊28at.com

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

圖片圖片sld28資訊網(wǎng)——每日最新資訊28at.com

圖片圖片sld28資訊網(wǎng)——每日最新資訊28at.com

圖片圖片sld28資訊網(wǎng)——每日最新資訊28at.com

圖片圖片sld28資訊網(wǎng)——每日最新資訊28at.com

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

sld28資訊網(wǎng)——每日最新資訊28at.com

總結

當一個線程池里面的線程異常后:sld28資訊網(wǎng)——每日最新資訊28at.com

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

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

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

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

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

下一篇: Go必知必會:數(shù)組和切片詳解

標簽:
  • 熱門焦點
Top