Tag: 完成服务

什么时候应该使用ExecutorService上的CompletionService?

我在这个博客文章中发现了CompletionService。 但是,这并没有真正展示CompletionService比标准ExecutorService更有优势。 相同的代码也可以写入。 那么,CompletionService何时有用呢? 你可以给一个简短的代码样本,使其透明? 例如,这个代码示例只显示不需要CompletionService(=相当于ExecutorService) ExecutorService taskExecutor = Executors.newCachedThreadPool(); // CompletionService<Long> taskCompletionService = // new ExecutorCompletionService<Long>(taskExecutor); Callable<Long> callable = new Callable<Long>() { @Override public Long call() throws Exception { return 1L; } }; Future<Long> future = // taskCompletionService.submit(callable); taskExecutor.submit(callable); while (!future.isDone()) { // Do some work… System.out.println("Working on something…"); } try { System.out.println(future.get()); } […]