gpt4 book ai didi

java - ScheduledExecutorService 不会因未捕获的异常而停止线程

转载 作者:行者123 更新时间:2023-11-30 08:09:26 24 4
gpt4 key购买 nike

来自这里的文档: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html#scheduleAtFixedRate(java.lang.Runnable,%20long,%20long,%20java.util.concurrent.TimeUnit)

内容如下:

If any execution of the task encounters an exception, subsequent executions are suppressed.

是否有任何等效的 ScheduledExecutorService.scheduleAtFixedRate 在未捕获的异常后不会关闭?

最佳答案

正如 Andy Turner 提到的,您可以使用“Catch-All”-Wrapper:

public class CatchAllRunnable implements Runnable{
private final Runnable wrappee;

public CatchAllRunnable( Runnable r ){ wrappee = r; }

@Override
public void run(){
try{ wrappee.run(); }
catch( Exception e ){ /* LOG e */ }
}
}

然后安排:

yourScheduledExecutorService.scheduleAtFixedRate( new CatchAllRunnable(originialRunnable), 1, TimeUnit.Minute);

时间当然根据您的要求调整。

关于java - ScheduledExecutorService 不会因未捕获的异常而停止线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32480075/

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