你可能应该在一个while(true)循环中包含try块,因为如果第一次运行没有抛出异常,你将退出你的方法,如果第二次调用抛出一个,你将无法捕获它.
我还会在自己的线程中运行递归调用,以避免在事情变坏时出现StackOverFlow错误的风险.
所以它看起来像这样:
private void startMemoryUpdateSchedule(final ScheduledExecutorService service) {
final ScheduledFuture> future = service.scheduleWithFixedDelay(new MemoryUpdateThread(), 1, UPDATE_MEMORY_SCHEDULE, TimeUnit.MINUTES);
Runnable watchdog = new Runnable() {
@Override
public void run() {
while (true) {
try {
future.get();
} catch (ExecutionException e) {
//handle it
startMemoryUpdateSchedule(service);
return;
} catch (InterruptedException e) {
//handle it
return;
}
}
}
};
new Thread(watchdog).start();
}