ScheduledExecutorService.scheduleWithFixedDelay(Runnable, long, long, TimeUnit)
throws RejectedExecutionException
(child of RuntimeException) ==> We can catch it and try sending again.
Now that future.get()
should return the result of one execution, we need to call it in a loop.
In addition, a failure of one execution does not affect the next scheduled execution, which distinguishes ScheduledExecutorService from TimerTask, which executes scheduled tasks in the same thread => failure in one execution would interrupt the schedule in case of TimerTask (http://stackoverflow.com / questions / 409932 / java-timer-vs-executorservice) We just need to catch all three exceptions thrown by Future.get (), but we wonβt be able to recover them, then we wonβt be able to get the result of subsequent executions.
The code may be:
public void startMemoryUpdateSchedule(final ScheduledExecutorService service) { final ScheduledFuture<?> future; try { future = service.scheduleWithFixedDelay(new MemoryUpdateThread(), 1, UPDATE_MEMORY_SCHEDULE, TimeUnit.SECONDS); } catch (RejectedExecutionException ree) { startMemoryUpdateSchedule(service); return; } while (true) { try { future.get(); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); } catch (ExecutionException ee) { Throwable cause = ee.getCause();
source share