CompleteableFuture异步编程框架

一、CompleteableFuture

CompleteableFuture是Java 8中的一个类,它提供了一种异步编程的方式。使用CompleteableFuture可以提高程序的并发性和性能。

二、案例引出

2.1 案例代码

  1. 目录结构
    在这里插入图片描述

  2. 有3个被调用的service(MemberService,OrderService,ProductService),每个service中有个search方法,分别耗时5,4,3秒钟。
    在这里插入图片描述

  3. 测试代码,需要计算查询需求的总体耗时
    在这里插入图片描述

2.2 引出CompleteableFuture异步调用

有一个查询需求,需要分别调用这3个service的search方法,获取对应会员、订单、产品列表。这3个调用的返回结果之间没有依赖关系。

  1. 串行调用
    public void query1(){
        List<Member> members = memberService.search();
        List<Order> orders = orderService.search();
        List<Product> products = productService.search();
    }

耗时为3+4+5=12秒

在这里插入图片描述

  1. 并行调用–也就是异步调用
    public void query2() throws ExecutionException, InterruptedException {
        CompletableFuture<List<Member>> memberFuture = CompletableFuture.supplyAsync(() -> memberService.search());
        CompletableFuture<List<Order>> orderFuture = CompletableFuture.supplyAsync(() -> orderService.search());
        CompletableFuture<List<Product>> productFuture = CompletableFuture.supplyAsync(() -> productService.search());
        CompletableFuture.allOf(memberFuture,orderFuture,productFuture).get();
    }

耗时为max{5,4,3}=5秒

在这里插入图片描述

三、CompletableFuture常用方法

1. supplyAsync( )

开启一个异步调用,并返回一个CompletableFuture对象

    public void query2(){
        CompletableFuture<List<Member>> memberFuture = CompletableFuture.supplyAsync(() -> memberService.search());
        CompletableFuture<List<Order>> orderFuture = CompletableFuture.supplyAsync(() -> orderService.search());
        CompletableFuture<List<Product>> productFuture = CompletableFuture.supplyAsync(() -> productService.search());
    }

在这里插入图片描述

2. runAsync( )

    public void query2(){
        CompletableFuture.runAsync(() -> memberService.search());
        CompletableFuture.runAsync(() -> orderService.search());
        CompletableFuture.runAsync(() -> productService.search());
    }

在这里插入图片描述

3. get( )

阻塞式地获取一个CompletableFuture对象的结果,抛出ExecutionException, InterruptedException异常

    public void query2() throws ExecutionException, InterruptedException {
        CompletableFuture<List<Member>> memberFuture = CompletableFuture.supplyAsync(() -> memberService.search());
        List<Member> members = memberFuture.get();
    }

在这里插入图片描述

4.链式调用–thenApply( )

返回的结果如果是CompletableFuture,可以继续调用相关api

thenApply( )方法:前面任务执行完后执行当前任务。消费上一次调用的结果,返回另一个结果

    public void query2() throws ExecutionException, InterruptedException {
        CompletableFuture<List<Order>> future = CompletableFuture.supplyAsync(() -> memberService.search())
                .thenApply(lastResult -> orderService.search());
        List<Order> orders = future.get();
    }

在这里插入图片描述

5. thenRun( )

前面任务执行完后执行当前任务。不管上一次调用的结果是什么,没有参数,没有返回值

    public void query2() throws ExecutionException, InterruptedException {
        CompletableFuture.supplyAsync(() -> memberService.search())
                .thenRun(() -> orderService.search());
    }

在这里插入图片描述

6. thenAccept( )

前面任务执行完后执行当前任务。消费上一次调用的结果,没有返回值

    public void query2() throws ExecutionException, InterruptedException {
        CompletableFuture.supplyAsync(() -> memberService.search())
                .thenAccept(lastResult -> orderService.search());
    }

在这里插入图片描述

7. anyOf( )

将多个future对象合并,返回最早运行结束的对象

    public void query2() throws ExecutionException, InterruptedException {
        CompletableFuture<List<Member>> memberFuture = CompletableFuture.supplyAsync(() -> memberService.search());
        CompletableFuture<List<Order>> orderFuture = CompletableFuture.supplyAsync(() -> orderService.search());
        CompletableFuture<List<Product>> productFuture = CompletableFuture.supplyAsync(() -> productService.search());
        CompletableFuture<Object> future = CompletableFuture.anyOf(memberFuture, orderFuture, productFuture);
        System.out.println(future.get());
    }

在这里插入图片描述

8. allOf( )

等待所有并行任务执行后,再执行后面的操作。
allOf( ):返回CompletableFuture<Void>对象,本身并不会阻塞

示例:等待memberService和orderService都结束后,再执行后面的异步操作。

    public void query2(){
        long a = System.currentTimeMillis();
        CompletableFuture<List<Member>> memberFuture = CompletableFuture.supplyAsync(() -> memberService.search());
        CompletableFuture<List<Order>> orderFuture = CompletableFuture.supplyAsync(() -> orderService.search());

        CompletableFuture<Void> allFutures = CompletableFuture.allOf(memberFuture, orderFuture);
        allFutures.thenRun(()->{
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            long b = System.currentTimeMillis();
            System.out.println("耗时111:"+(b-a));
        });
    }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值