设置自动保存计时器以在java中定期保存文件(但不使用任何JavaFXAPI)的最佳策略是什么?
假设我需要每隔15分钟自动保存一个文件
我把How to set a Timer in Java?作为参考
有时机器会进入省电模式
应用程序恢复运行后,计时器开启的线程需要从断电暂停之前的时间恢复,但不能从头开始重新启动
在这种情况下,ExecutorService和Future的使用会是最好的吗?如何使用
当我捕获以下任何异常时,我该怎么做才能恢复和恢复自动保存计时器
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();
}
共 (0) 个答案