CompletionService的介绍和使用

public interface CompletionService<V>
 
 

将生产新的异步任务与使用已完成任务的结果分离开来的服务。生产者 submit 执行的任务。使用者 take 已完成的任务,并按照完成这些任务的顺序处理它们的结果。例如,CompletionService 可以用来管理异步 IO ,执行读操作的任务作为程序或系统的一部分提交,然后,当完成读操作时,会在程序的不同部分执行其他操作,执行操作的顺序可能与所请求的顺序不同。

通常,CompletionService 依赖于一个单独的 Executor 来实际执行任务,在这种情况下,CompletionService 只管理一个内部完成队列。ExecutorCompletionService 类提供了此方法的一个实现。

内存一致性效果:线程中向 CompletionService 提交任务之前的操作 happen-before 该任务执行的操作,后者依次 happen-before 紧跟在从对应 take() 成功返回的操作。

// 这个东西的使用上很类似于CallableFutureTest,不同的是,它会首先取完成任务的线程。
public class CompletionServiceTest {
    public static void main(String[] args) throws InterruptedException,  
    ExecutionException {
        ExecutorService exec = Executors.newFixedThreadPool(10);  
        // 创建CompletionService
        CompletionService<String> serv =  
        new ExecutorCompletionService<String>(exec);
        for (int index = 0; index < 5; index++) {  
            final int NO = index;  
            // Callable 接口类似于 Runnable
            Callable<String> downImg = new Callable<String>() {  
                public String call() throws Exception {  
                    Thread.sleep((long) (Math.random() * 10000));  
                    return "Downloaded Image " + NO;  
                }  
            };
            // 提交要执行的值返回任务,并返回表示挂起的任务结果的 Future。在完成时,可能会提取或轮询此任务。
            serv.submit(downImg);  
        }
        Thread.sleep(1000 * 2);  
        System.out.println("Show web content");
        for (int index = 0; index < 5; index++) {
            // 获取并移除表示下一个已完成任务的 Future,如果目前不存在这样的任务,则等待。
            Future<String> task = serv.take();
            // 如有必要,等待计算完成,然后获取其结果。
            String img = task.get();
            System.out.println(img);
        }
        System.out.println("End");
        // 关闭线程池 
        exec.shutdown();  
    }  
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值