Why does a ScheduledExecutorService not run a task again after an exception is thrown?

。_饼干妹妹 提交于 2019-12-03 09:37:23
Peter Lawrey

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 points out in the comment above,

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.

matt b has given the reason.

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.



It should be noted that is written in the documentation of ScheduledExecutorService

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

And as Michael Krusse says, the point about creating a new thread is to allow other tasks to continue running.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!