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

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

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

一個(gè)線程池中的線程異常了,那么線程池會(huì)怎么處理這個(gè)線程?

需要說(shuō)明,本文的線程池都是java.util.concurrent.ExecutorService線程池,本文將圍繞驗(yàn)證,閱讀源碼倆方面來(lái)解析這個(gè)問題。

代碼驗(yàn)證

驗(yàn)證execute提交線程池中

測(cè)試代碼:

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

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

圖片圖片

結(jié)論:

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

驗(yàn)證submit提交線程池中

測(cè)試代碼:

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

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

圖片圖片

結(jié)論:

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

源碼解析

1.java.util.concurrent.AbstractExecutorService#submit(java.lang.Runnable);

圖片圖片

2. 查看execute方法的執(zhí)行邏輯;

圖片圖片

3. java.util.concurrent.ThreadPoolExecutor#processWorkerExit;

圖片圖片

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

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

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

圖片圖片

圖片圖片

圖片圖片

圖片圖片

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

總結(jié)

當(dāng)一個(gè)線程池里面的線程異常后:

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

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

責(zé)任編輯:武曉燕 來(lái)源: 一安未來(lái)
相關(guān)推薦

2024-04-02 09:53:08

線程池線程堆棧

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è)計(jì)電腦Java

2020-02-26 15:12:43

線程池增長(zhǎng)回收

2023-10-26 08:25:35

Java線程周期

2022-09-29 09:19:04

線程池并發(fā)線程

2011-06-01 11:23:09

Android 線程

2021-06-17 06:57:10

SpringBoot線程池設(shè)置

2021-06-11 11:28:22

多線程fork單線程

2024-11-06 12:59:42

多線程銷毀線程切換

2012-02-21 14:14:47

Java

2010-02-24 11:19:00

Python主線程

2012-01-16 09:00:56

線程

2024-04-08 10:09:37

TTLJava框架

2022-06-24 06:43:57

線程池線程復(fù)用

2009-08-12 13:22:44

Singleton模式

2022-10-12 09:01:52

Linux內(nèi)核線程
點(diǎn)贊
收藏

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