"線程池中線程異常后:銷毀還是復(fù)用?"
一、一個線程池中的線程異常了,那么線程池會怎么處理這個線程?
需要說明,本文的線程池都是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é)論:
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
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í)行。