CompletableFuture使用

使用CompletableFuture替代Thread+Runnable

解决Thread+Runnable没有返回值、串行化和组合任务问题

1、CompletableFuture.runAsync 异步没有返回值

需要传入一个线程池

public static ExecutorService executor = Executors.newFixedThreadPool(10);
CompletableFuture.runAsync(()->{
   System.out.println("当前线程:"+Thread.currentThread().getId());
   int i = 10/2;
   System.out.println("运行结果:"+ i );
},executor);

2、CompletableFuture.supplyAsync 异步有返回值

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
     System.out.println("当前线程:" + Thread.currentThread().getId());
     int i = 10 / 2;
     System.out.println("运行结果:" + i);
     return i;
}, executor).handle((res,thr)->{//结果处理,res 结果,thr 异常
     if(res !=null){
         return res*2;
     }
     if(thr!=null){
         return 0;
     }
     return 0;
});
Integer i = future.get();// 阻塞

3、线程串行化

没有接收结果

         /**
         * 线程串行化
         */
        CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果:" + i);
            return i;
        }, executor).thenRunAsync(() -> {
            System.out.println("任务2执行成功");
        }, executor);

有接收结果

        CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果:" + i);
            return i;
        }, executor).thenAcceptAsync((res)->{
            System.out.println("任务2启动了.."+res);
        },executor);

接收结果,并修改返回结果

        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果:" + i);
            return i;
        }, executor).thenApplyAsync((res) -> {
            System.out.println("任务2启动了...");
            return "hello " + res;
        }, executor);
        String s = future.get(); // 阻塞

4、组合任务-都要完成

需等组合任务完成,第三个线程才开始

        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1启动了...");
            return 5;
        }, executor);

        CompletableFuture<Integer> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2启动了...");
            return 5;
        }, executor);

        future01.runAfterBothAsync(future02,()->{
            System.out.println("任务3开始...");
        },executor);

组合任务都完成,感知结果

        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1启动了...");
            return 5;
        }, executor);

        CompletableFuture<Integer> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2启动了...");
            return 5;
        }, executor);

        future01.thenAcceptBothAsync(future02,(f1,f2)->{
            System.out.println("任务3开始...之前的结果:"+f1+"-> "+ f2);
        },executor);

组合任务都完成,感知结果,并重新返回值

        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1启动了...");
            return 5;
        }, executor);

        CompletableFuture<Integer> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2启动了...");
            return 5;
        }, executor);

        CompletableFuture<Integer> future = future01.thenCombineAsync(future02, (f1, f2) -> {
            System.out.println("任务3开始...之前的结果:" + f1 + "-> " + f2);
            return f1 + f2;
        }, executor);

        Integer i = future.get();

5、组合任务-一个完成

两个任务,只要有一个完成,我们就执行任务3。

没有感知结果,没有返回值

        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1启动了...");
            return 5;
        }, executor);

        CompletableFuture<Integer> future02 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
                System.out.println("任务2启动了...");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 5;
        }, executor);

        future01.runAfterEitherAsync(future02,()->{
            System.out.println("任务3启动了...");
        },executor);

感知结果

        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1启动了...");
            return 5;
        }, executor);

        CompletableFuture<Integer> future02 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
                System.out.println("任务2启动了...");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 5;
        }, executor);

        /**
         * 需相同返回类型
         */
        future01.acceptEitherAsync(future02,(res)->{
            System.out.println("任务3启动了...");
        },executor);

感知结果,并且有返回值

        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1启动了...");
            return 5;
        }, executor);

        CompletableFuture<Integer> future02 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
                System.out.println("任务2启动了...");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 5;
        }, executor);

        /**
         * 需相同返回类型
         */
        CompletableFuture<Integer> future = future01.applyToEitherAsync(future02, (res) -> {
            System.out.println("任务3启动了...");
            return res + 1;
        }, executor);

        Integer i = future.get();

6、多任务组合(常用)

allOf:等待所有任务完成

anyOf:只要有一个任务完成

allOf,等待所有任务完成

        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的图片信息");
            return "hello.jpg";
        }, executor);

        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的属性");
            return "黑色+256G";
        }, executor);

        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("查询商品介绍");
            return "华为";
        }, executor);

        CompletableFuture<Void> allOf = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
        allOf.get(); // 阻塞等待所有线程完成
        String s = futureImg.get();
        String s1 = futureAttr.get();
        String s2 = futureDesc.get();

anyOf,只要有一个任务完成

       CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的图片信息");
            return "hello.jpg";
        }, executor);

        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的属性");
            return "黑色+256G";
        }, executor);

        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("查询商品介绍");
            return "华为";
        }, executor);

        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
        anyOf.get(); // 阻塞等待所有线程完成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值