【发布时间】:2015-01-29 15:40:38
【问题描述】:
我正在将文件发送到创建文件的本地服务器。我的问题发生在用户一个接一个地执行多项操作时,如果其中一个请求在 5 分钟内没有收到反馈文件,我需要显示一条错误消息。
如何处理所有这些请求?我使用newSingleThreadScheduledExecutor 来检查反馈文件是否每分钟都存在,但我不知道如何处理多个反馈文件并将每个请求的倒计时保持在 5 分钟的情况下。
我的尝试:
ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(listPrinter.size()));
for(int i=0;i<list.size();i++){
try {
final File retrievedFile = new File("/home/"+list.get(i)+".csv");
ListenableFuture<File> future = executor.submit(new Callable<File>() {
public File call() {
// Actually send the file to your local server
// and retrieve a file back
if(retrievedFile.exists())
{
new Notification("file exits").show(Page.getCurrent());
}
else{
new Notification("file no exits").show(Page.getCurrent());
}
return retrievedFile;
}
});
future.get(5, TimeUnit.MINUTES);
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
} catch (ExecutionException ex) {
Exceptions.printStackTrace(ex);
} catch (TimeoutException ex) {
Exceptions.printStackTrace(ex);
new Notification("Time out").show(Page.getCurrent());
}
}
但它只是在开始时执行,仅此而已,但添加文件时没有任何反应。
watchService 可以做到这一点吗?它对我来说效果很好,但我不知道 5 分钟的情况
【问题讨论】:
标签: java multithreading timer executor