Jak zrestartować harmonogram, gdy harmonogramWithFixedDelay zgłasza wyjątek?
używamScheduledExecutorService
zaplanować niektóre zadania, które muszą być uruchamiane okresowo. Chcę wiedzieć, czy ten kod działa w celu odzyskania harmonogramu, gdy wystąpi wyjątek.
ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
this.startMemoryUpdateSchedule(service);//See below method
//Recursive method to handle exception when run schedule task
private void startMemoryUpdateSchedule(ScheduledExecutorService service) {
ScheduledFuture<?> future = service.scheduleWithFixedDelay(new MemoryUpdateThread(), 1, UPDATE_MEMORY_SCHEDULE, TimeUnit.MINUTES);
try {
future.get();
} catch (ExecutionException e) {
e.printStackTrace();
logger.error("Exception thrown for thread",e);
future.cancel(true);
this.startMemoryUpdateSchedule(service);
} catch(Exception e) {
logger.error("Other exception ",e);
}
}