gpt4 book ai didi

java - 如果发生异常,ThreadPoolExecutor::afterExecute 会返回可运行吗?

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

ThreadPoolExecutor 的 Javadoc 定义 ( https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html#afterExecute(java.lang.Runnable,%20java.lang.Throwable) )

protected void afterExecute(Runnable r, Throwable t)

发生异常时初始可运行对象是否会传递给函数?

最佳答案

已返回。这很容易检查 - 考虑基于文档的代码:

public class MainApp {

public static void main(String[] args) {
testme();
}

public static void testme() {
ThreadPoolExecutor myown = new ExtendedExecutor(2,4,10, TimeUnit.DAYS.SECONDS, new ArrayBlockingQueue<Runnable>(2));

myown.execute(() -> {
throw new RuntimeException("Something went wrong");
// System.out.println("Hey there");
}
);
}


static class ExtendedExecutor extends ThreadPoolExecutor {

public ExtendedExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
}

protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (t == null && r instanceof Future<?>) {
try {
Object result = ((Future<?>) r).get();
} catch (CancellationException ce) {
t = ce;
} catch (ExecutionException ee) {
t = ee.getCause();
} catch (InterruptedException ie) {
Thread.currentThread().interrupt(); // ignore/reset
}
}
if (t != null) {
System.out.println("We've got error");
System.out.println(r==null?"null":"not null");
}
}
}

关于java - 如果发生异常,ThreadPoolExecutor::afterExecute 会返回可运行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43521456/

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