【发布时间】:2016-12-26 21:12:02
【问题描述】:
我有一个程序,其中我使用提交给 ExecutorService 的 Callable 对象从 S3 下载文件。文件很大,需要几分钟才能完全下载。创建另一个从下载器获取未来并观察它完成的 Callable 类是否有意义?我的最终目标是将所有完整下载添加到缓存中位于中心位置的列表中。
例如:
public void add(final String s3urlToDownload){
Future<S3Object> futureS3obj = cachedPoolExecutor.submit(new S3Downloader(s3urlToDownload));
// Instead of calling futureS3obj.get() and waiting, submit the Future to the "Watcher" service.
// Within FutureWatcher, the S3Object will be added to the List once the download is complete.
cachedPoolExecutor.submit(new FutureWatcher(downloadedList, futureS3obj))
}
【问题讨论】:
-
如果您已经拥有
Future,那么额外的Callable有什么好处? -
@Kayaman,据我了解,如果我调用
Future的get()方法,add方法将阻塞。这就是为什么我提交一个新的 CallableFutureWatcher -
那么
Callable带来了什么?调用它的call()方法也会阻塞,因为它委托给get()。 -
这个准确吗:有N个文件要下载。它们应该被并行下载,并且需要一种机制来知道什么时候都准备好了?如果是这样,可能还有其他方法可以解决这个问题(如果这是准确的,我可能会尝试一个答案)。
-
@MichaelEaster,没错。
标签: java future executorservice callable