且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么在抛出异常之后,ScheduledExecutorService不再运行任务?

更新时间:2023-12-04 09:34:04

一旦重复的任务抛出一个未捕获的异常,死亡或处于错误状态,它有点麻烦,它也会默默地失败,除非你检查未来获得错误/异常。

Once a repeating task has thrown an uncaught exception it is assumed to have died or be in an error state. It is a bit of a gotcha that it also fails silently unless you examine the Future to get the Error/Exception.

你必须抓住例外,如果你不想杀死重复的任务。

You have to catch Exceptions if you don't want to kill the repeating task.

as matt b指出上面的评论,


这样的框架代码假设它可以安全地重新启动失败的工作将是有问题的 - 事实上它失败了一个异常意味着数据可能已经处于任何状态,并且可能重新启动该作业是不安全的。

it would be problematic for framework code like this to assume it can safely restart a failed job - the fact that it failed with an exception means that the data might have been left in any sort of state, and potentially it would be unsafe to restart the job.