自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

"線程池中線程異常后:銷毀還是復(fù)用?"

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

一、一個線程池中的線程異常了,那么線程池會怎么處理這個線程?

需要說明,本文的線程池都是java.util.concurrent.ExecutorService線程池,本文將圍繞驗證,閱讀源碼倆方面來解析這個問題。

二、代碼驗證

2.1 驗證execute提交線程池中

2.1.1 測試代碼:

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í)行任務(wù)=======================");

        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);
        }
    }
}

2.1.2 執(zhí)行結(jié)果如下:

2.1.3 結(jié)論:

execute 提交到線程池的方式,如果執(zhí)行中拋出異常,并且沒有在執(zhí)行邏輯中catch,那么會拋出異常,并且移除拋出異常的線程,創(chuàng)建新的線程放入到線程池中。

2.2 驗證submit提交線程池中

2.2.1 測試代碼:

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í)行任務(wù)=======================");

        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);
        }
    }
}

2.2.2 執(zhí)行結(jié)果如下:

2.2.3 結(jié)論:

submit 提交到線程池的方式,如果執(zhí)行中拋出異常,并且沒有catch,不會拋出異常,不會創(chuàng)建新的線程。

三、源碼解析

3.1java.util.concurrent.AbstractExecutorService#submit(java.lang.Runnable)

3.2 查看execute方法的執(zhí)行邏輯java.util.concurrent.ThreadPoolExecutor#runWorker

3.3 java.util.concurrent.ThreadPoolExecutor#processWorkerExit

可以發(fā)現(xiàn),如果拋出異常,會移除拋出異常的線程,創(chuàng)建新的線程。

3.4 為什么submit方法,沒有創(chuàng)建新的線程,而是繼續(xù)復(fù)用原線程?

還記得,我們在3.1的時候,發(fā)現(xiàn)submit也是調(diào)用了execute方法,但是在調(diào)用之前,包裝了一層 RunnableFuture,那一定是在RunnableFuture的實現(xiàn) FutureTask中有特殊處理了,我們查看源碼可以發(fā)現(xiàn)。

但是,我們通過java.util.concurrent.FutureTask#get(),就可以獲取對應(yīng)的異常信息。

四、總結(jié)

當一個線程池里面的線程異常后:

?當執(zhí)行方式是execute時,可以看到堆棧異常的輸出,線程池會把這個線程移除掉,并創(chuàng)建一個新的線程放到線程池中。

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

以上倆種執(zhí)行方式,都不會影響線程池里面其他線程的正常執(zhí)行。

責任編輯:武曉燕 來源: 今日頭條
相關(guān)推薦

2024-06-13 09:30:33

Java線程池線程

2024-08-29 08:54:35

2023-02-02 08:56:25

線程池線程submit

2025-02-05 14:28:19

2024-10-11 16:57:18

2019-09-26 10:19:27

設(shè)計電腦Java

2020-02-26 15:12:43

線程池增長回收

2011-06-01 11:23:09

Android 線程

2021-06-17 06:57:10

SpringBoot線程池設(shè)置

2022-09-29 09:19:04

線程池并發(fā)線程

2024-11-06 12:59:42

多線程銷毀線程切換

2010-02-24 11:19:00

Python主線程

2012-01-16 09:00:56

線程

2024-04-08 10:09:37

TTLJava框架

2023-10-26 08:25:35

Java線程周期

2022-06-24 06:43:57

線程池線程復(fù)用

2012-02-21 14:14:47

Java

2009-08-12 13:22:44

Singleton模式

2022-10-12 09:01:52

Linux內(nèi)核線程

2021-06-11 11:28:22

多線程fork單線程
點贊
收藏

51CTO技術(shù)棧公眾號