zoukankan      html  css  js  c++  java
  • 批量执行异步任务CompletionService

    批量执行异步任务CompletionService

    核心思想,就是将异步结果放入到阻塞队列中,然后再消费队列,实现异步任务批量执行
    接口方法说明

    
    Future<V> submit(Callable<V> task);
    Future<V> submit(Runnable task, V result);
    Future<V> take() 
      throws InterruptedException // 阻塞式调用;
    Future<V> poll();
    Future<V> poll(long timeout, TimeUnit unit) 
      throws InterruptedException;
    
    public static void test() throws InterruptedException, ExecutionException {
            ExecutorService executor = Executors.newFixedThreadPool(3);
            CompletionService<Integer> cs = new ExecutorCompletionService<Integer>(executor);
            cs.submit(() -> getPriceByS1());
            cs.submit(() -> getPriceByS2());
            cs.submit(() -> getPriceByS3());
            for (int i = 0; i < 3; i++) {
                Integer r = cs.take().get();
                executor.execute( () -> System.out.println("save:" + r));
            }
    }
    
    public static Integer getPriceByS1(){
            return 1;
    }
    public static Integer getPriceByS2(){
            return 2;
    }
    public static Integer getPriceByS3(){
            return 3;
    }
    
    原创:做时间的朋友
  • 相关阅读:
    ES6 数值类型常用方法
    阿里云如何发布网站
    常用的网站推荐
    笔记一 sql 基础知识
    笔记一 MVC初识
    基础二 面向对象编程
    基础一
    css reset 样式
    CSS 嵌套绝对定位
    ADO
  • 原文地址:https://www.cnblogs.com/PythonOrg/p/14412822.html
Copyright © 2011-2022 走看看