gpt4 book ai didi

java - 如果将来的任务抛出异常,线程是否返回到执行器服务的线程池?

转载 作者:行者123 更新时间:2023-11-30 07:36:15 25 4
gpt4 key购买 nike

我有 ThreadPoolExecutor 的简单用例。我想确保在线程执行期间如果出现任何类型的运行时异常,线程是否会返回到池中。如何验证?

ExecutorService pool = Executors.newFixedThreadPool(30);
pool.submit(new MyThread())
boolean jobExecutionStatus =true
try{
for (Future<Boolean> future : futureArrayList){
if (jobExecutionStatus){
jobExecutionStatus = future.get();
}
else{
break;
}

}
}
catch (InterruptedException | ExecutionException e){
jobExecutionStatus = false;
}

最佳答案

不,如果抛出异常,则会创建新的线程(仅在必要时)。

来自javadoc :

If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.

关于java - 如果将来的任务抛出异常,线程是否返回到执行器服务的线程池?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35358787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com