CompletableFuture中,怎样确保异步执行的thenAccept能执行完

在我们的日常开发中我们会经常用到异步代码来提升我们api的性能,也有这样的场景,比如我们在A异步操作后,取得A的结果后,又需要进行B操作。借助java8的CompletableFuture,代码如下

public class Java8Test {
    public static void main(String[] args){
        CompletableFuture firstFuture = CompletableFuture.supplyAsync(()->{
            System.out.println("start to execute supplyAsync");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("end to execute supplyAsync");
            return "firstFuture";

        }).thenAccept(otxGroupData -> {
            System.out.println("start to execute thenAccept");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("end to execute thenAccept");
        }).exceptionally((e) -> {

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e1) {
                e.printStackTrace();
            }
            return null;
        });

        System.out.println("main end");

    }
}

可以看到程序会先执行supplyAsync,再执行thenAccept,这样会有一个疑问,如果还没执行完thenAccept,主线程这是执行完退出了,会影响到我们的thenAccept执行吗?先看运行结果:
在这里插入图片描述
可以看到主线程执行完提出了,并没有等待thenAccept的执行。这是我们可以在主线程加上firstFuture.get();这句代码,get()会阻塞在这儿等待整个firstFuture的流程都执行完成,然后才回到主线程执行。

public class Java8Test {
    public static void main(String[] args){
        CompletableFuture firstFuture = CompletableFuture.supplyAsync(()->{
            System.out.println("start to execute supplyAsync");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("end to execute supplyAsync");
            return "firstFuture";

        }).thenAccept(otxGroupData -> {
            System.out.println("start to execute thenAccept");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("end to execute thenAccept");
        }).exceptionally((e) -> {

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e1) {
                e.printStackTrace();
            }
            return null;
        });

        try {
            firstFuture.get();
            System.out.println("main end");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

运行结果:
在这里插入图片描述
这样就不用担心thenAccept执行不到了。CompletableFuture的异步的功能还很强大,自己也在学习中,后面再来总结,未完待续。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值