java 執行緒池 異常 處理 機制 分析

2021-09-01 13:12:18 字數 743 閱讀 5785

public class threadtest },"success");

trycatch (exception e)}}

上述**只有在呼叫get()時丟擲異常,否則不列印任何異常資訊,檢視原始碼得到原因如下:

public futuresubmit(runnable task, v result)

executorcompletionservice 的submit方法將runnable 包裝為乙個runnablefuture,而runnablefuture的實現類為futuretask,futuretask的run方法如下:

public void run()

void innerrun() catch (throwable ex)

set(result);

} else

}

可以看出當程式丟擲異常時,沒有列印任何異常資訊,只是呼叫setexception(ex),將異常資訊賦值給 exception變數。當呼叫future.get()方法時,丟擲異常,**如下:

v innerget() throws interruptedexception, executionexception

解析Java執行緒池的異常處理機制

今天小夥伴遇到個小問題,執行緒池提交的任務如果沒有catch異常,那麼會拋到 去,之前倒是沒研究過,本著實事求是的原則,看了一下 考慮下面這段 有什麼區別呢?你可以猜猜會不會有異常打出呢?如果打出來的話是在 executorservice threadpool executors.newfixedt...

深度解析Java執行緒池的異常處理機制

今天小夥伴遇到個小問題,執行緒池提交的任務如果沒有catch異常,那麼會拋到 去,之前倒是沒研究過,本著實事求是的原則,看了一下 考慮下面這段 有什麼區別呢?你可以猜猜會不會有異常打出呢?如果打出來的話是在 executorservice threadpool executors.newfixedt...

java之異常處理機制

class myexception extends exception myexception string msg,int id void method throws myexception public class testexception catch arithmeticexception ...