Say I need to autosave a file every 15 minutes periodically.
I've looked at How to set a Timer in Java? as a reference.
There are times the machine would be putting under the power saving mode.
After the app is being brought back running, the thread that the timer is on will need to be resumed from the time elapsed before being suspended from the power saving but NOT restarting it from scratch.
Will the use of ExecutorService and Future be the best in this case and how ?
What do I do when I capture any of the following exceptions so that I can resume and reinstate the autosave timer ?
ExecutorService service = Executors.newSingleThreadExecutor();
try {
Runnable r = new Runnable() {
@Override
public void run() {
saveFile();
}
};
Future<?> f = service.submit(r);
f.get(15, TimeUnit.MINUTES); // attempt the task for 15 minutes
}
catch (final InterruptedException e) {
// The thread was interrupted during sleep, wait or join
// how to redo the countdown ?
}
catch (final TimeoutException e) {
// Took too long!
// what to do ?
}
catch (final ExecutionException e) {
// An exception from within the Runnable task
// what to do ?
}
finally {
service.shutdown();
}