• CompletableFuture实现异步获取结果并且等待所有异步任务完成


    对于的使用可以参照这篇文章:https://blog.csdn.net/jianjun200607/article/details/83996833

    下面主要用代码的方式描述下通过CompletableFuture实现异步获取结果并且等待所有异步任务完成:

    /**
    * CompletableFuture的AllOf功能测试,等待所有任务执行完
    *
    */
    public class CompletableFutureAllOfTest {

    public static void main(String[] args) throws Exception {
    ExecutorService executor = ExecutorServiceSupport.newTheadPool("completableFuture_allOf_test");
    method1(executor);
    method2(executor);
    method3(executor);
    }

    /**
    * 拆解写法
    * @param executor
    */
    public static void method1 (ExecutorService executor) {
    long start = System.currentTimeMillis();
    // 定义第一个任务
    CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {
    try {
    Thread.sleep(3000);
    } catch (Exception e) {
    e.printStackTrace();
    }

    return "cf1";
    }, executor);

    cf1.whenComplete(new BiConsumer<String, Throwable>() {
    @Override
    public void accept(String t, Throwable u) {
    System.out.println("hello " + t);
    }
    });

    // 定义第二个任务
    CompletableFuture<String> cf2 = CompletableFuture.supplyAsync(() -> {
    try {
    Thread.sleep(5000);
    } catch (Exception e) {
    e.printStackTrace();
    }

    return "cf2";
    }, executor);

    cf2.whenComplete(new BiConsumer<String, Throwable>() {
    @Override
    public void accept(String t, Throwable u) {
    System.out.println("hello " + t);
    }
    });
    // 开始等待所有任务执行完成
    CompletableFuture<Void> all = CompletableFuture.allOf(cf1, cf2);
    System.out.println("start block");
    all.join();
    System.out.println("block finish, consume time:" + (System.currentTimeMillis() - start));
    }

    /**
    * 合并写法
    * @param executor
    */
    public static void method2 (ExecutorService executor) {
    List<String> testList = Lists.newArrayList();
    testList.add("cf1");
    testList.add("cf2");
    long start = System.currentTimeMillis();
    CompletableFuture<Void> all = null;
    for (String str : testList) {
    // 定义任务
    CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> {
    try {
    Thread.sleep(3000);
    } catch (Exception e) {
    e.printStackTrace();
    }

    return str;
    }, executor);

    cf.whenComplete(new BiConsumer<String, Throwable>() {
    @Override
    public void accept(String t, Throwable u) {
    System.out.println("hello " + t);
    }
    });
    all = CompletableFuture.allOf(cf);
    }
    System.out.println("start block");
    // 开始等待所有任务执行完成
    all.join();
    System.out.println("block finish, consume time:" + (System.currentTimeMillis() - start));
    }

    /**
    * 通过Java8的stream实现,非常简洁
    * @param executor
    */
    @SuppressWarnings("rawtypes")
    public static void method3 (ExecutorService executor) {
    List<String> testList = Lists.newArrayList();
    testList.add("cf1");
    testList.add("cf2");
    long start = System.currentTimeMillis();
    CompletableFuture[] cfArr = testList.stream().
    map(t -> CompletableFuture
    .supplyAsync(() -> pause(t), executor)
    .whenComplete((result, th) -> {
    System.out.println("hello" + result);
    })).toArray(CompletableFuture[]::new);
    // 开始等待所有任务执行完成
    System.out.println("start block");
    CompletableFuture.allOf(cfArr).join();
    System.out.println("block finish, consume time:" + (System.currentTimeMillis() - start));
    }

    public static String pause (String name) {
    try {
    Thread.sleep(5000);
    } catch (Exception e) {
    e.printStackTrace();
    }

    return name;
    }

    }

    ————————————————
    版权声明:本文为CSDN博主「大军001」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/jianjun200607/article/details/84027273

  • 相关阅读:
    吃了很多杏仁,干果的祸
    persistent.xml hibernate 利用sql script 自定义生成 table 表
    JSF dataTable 添加列 动态创建数据表 列
    java 和 mysql 获取周 星期 的第一天 最后一天 或者 月的 日期(字符串转日期,日期转字符串,日期加减)
    JSF JQUERY 使用datepicker
    JPA mysql wildfly jboss 存储时乱码
    JPA事务总结
    这样吃饭,其实是在喂养身体里的“癌细胞”
    Mysql 列转行统计查询 、行转列统计查询
    mysql 生成排名字段
  • 原文地址:https://www.cnblogs.com/suizhikuo/p/15448717.html
Copyright © 2020-2023  润新知