CompletableFuture详解

CompletableFuture 详解

一个接口可能需要调用 N 个其他服务的接口,这在项目开发中还是挺常见的。举个例子:用户请求获取订单信息,可能需要调用用户信息、商品详情、物流信息、商品推荐等接口,最后再汇总数据统一返回。

如果是串行(按顺序依次执行每个任务)执行的话,接口的响应速度会非常慢。考虑到这些接口之间有大部分都是 无前后顺序关联 的,可以 并行执行 ,就比如说调用获取商品详情的时候,可以同时调用获取物流信息。通过并行执行多个任务的方式,接口的响应速度会得到大幅优化。

serial-to-parallelserial-to-parallel

对于存在前后顺序关系的接口调用,可以进行编排,如下图所示。

img

  1. 获取用户信息之后,才能调用商品详情和物流信息接口。
  2. 成功获取商品详情和物流信息之后,才能调用商品推荐接口。

对于 Java 程序来说,Java 8 才被引入的 CompletableFuture 可以帮助我们来做多个任务的编排,功能非常强大。

Future类

介绍CompletableFuture之前需要先了解Future类

Future 类是异步思想的典型运用,主要用在一些需要执行耗时任务的场景,避免程序一直原地等待耗时任务执行完成,执行效率太低。具体来说是这样的:当我们执行某一耗时的任务时,可以将这个耗时任务交给一个子线程去异步执行,同时我们可以干点其他事情,不用傻傻等待耗时任务执行完成。等我们的事情干完后,我们再通过 Future 类获取到耗时任务的执行结果。这样一来,程序的执行效率就明显提高了

简单来说可以将Future理解为烧水壶,我们在烧热水的过程中不需要等待它烧完,可以去做其它事情,等我们事情做完后再去看水烧完没

Future类的方法

在这里插入图片描述

  • cancel(boolean mayInterruptIfRunning),取消任务
  • isCancelled(),判断是否取消
  • isDone(),是否完成
  • get(),获取任务完成的结果,没获取到会阻塞
  • get(long timeout, TimeUnit unit),指定timeout内没有获取到结果就抛出TimeOutException异常
使用Future的局限性

Future的默认实现是FutureTask类

在这里插入图片描述

public class FutureExample {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService executor = Executors.newFixedThreadPool(2);
        Future<Integer> future1 = executor.submit(() -> {
            Thread.sleep(2000);
            return 1;
        });
        Future<Integer> future2 = executor.submit(() -> {
            Thread.sleep(1000);
            return 2;
        });

        // 阻塞调用,等待第一个任务完成
        int result1 = future1.get();
        System.out.println("Result of first task: " + result1);

        // 阻塞调用,等待第二个任务完成
        int result2 = future2.get();
        System.out.println("Result of second task: " + result2);

        executor.shutdown();
    }
}

上面的代码的缺点是什么?

  • 不支持异步任务的编排组合,通俗来讲就是必须等第一个任务的结果返回,才能获取第二个任务的结果;
  • get方法会阻塞,影响主线程执行
CompletableFuture介绍

CompletableFuture就是为了解决FutureTask类的缺点,CompletableFuture 除了提供了更为好用和强大的 Future 特性之外,还提供了函数式编程异步任务编排组合(可以将多个异步任务串联起来,组成一个完整的链式调用)等能力

CompletableFuture的结构

在这里插入图片描述

public class CompletableFuture<T> implements Future<T>, CompletionStage<T> {
}

可以看到CompletableFuture实现了Future以及CompletionStage接口

CompletionStage接口:描述了一个异步计算的阶段。很多计算可以分成多个阶段或步骤,此时可以通过它将所有步骤组合起来,形成异步计算的流水线,CompletableFuture的函数式编程能力就是它赋予的

实践

我们通过创建了一个结果值类型为 StringCompletableFuture,你可以把 resultFuture 看作是异步运算结果的载体

public class CompletableFutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //CompletableFuture<V> V是返回数据的类型
        CompletableFuture<String> resultFuture = new CompletableFuture<>();

        // 调用数据库...
        // 塞入结果,只能调用一次
        resultFuture.complete("从数据库获取到信息{张三,23,男}");
        resultFuture.complete("123"); // 无效

        if (resultFuture.isDone()) {
            System.out.println("完成辣");
        }

        System.out.println(resultFuture.get());
    }
}
静态工厂方法
// 异步返回结果,supplyAsync接收Supplier<U>,Supplier<U>与Runnable一样是函数式接口,U是结果的类型
static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier);
// 使用自定义线程池(推荐)
static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor);
// 异步不返回结果
static CompletableFuture<Void> runAsync(Runnable runnable);
// 使用自定义线程池(推荐)
static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor);

Supplier

@FunctionalInterface
public interface Supplier<T> {

    /**
     * Gets a result.
     * 获取返回结果
     * @return a result
     */
    T get();
}
public static void test2() throws ExecutionException, InterruptedException {
        // 自定义线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 4, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10));
        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> System.out.println("hello!"), threadPoolExecutor);
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "hello!", threadPoolExecutor);
        System.out.println("future2返回:" + future2.get());
        // 关闭线程池
        threadPoolExecutor.shutdown();
    }

关于自定义线程池好处的可以看:Java线程池-CSDN博客

运行结果

在这里插入图片描述

处理异步结算的结果

常用的异步处理方法

  • thenApply()
  • thenAccept()
  • thenRun()
  • whenComplete()
// 沿用上一个任务的线程池
public <U> CompletableFuture<U> thenApply(
    Function<? super T,? extends U> fn) {
    return uniApplyStage(null, fn);
}

//使用默认的 ForkJoinPool 线程池(不推荐)
public <U> CompletableFuture<U> thenApplyAsync(
    Function<? super T,? extends U> fn) {
    return uniApplyStage(defaultExecutor(), fn);
}
// 使用自定义线程池(推荐)
public <U> CompletableFuture<U> thenApplyAsync(
    Function<? super T,? extends U> fn, Executor executor) {
    return uniApplyStage(screenExecutor(executor), fn);
}

thenApply() 方法使用示例如下:

CompletableFuture<String> future = CompletableFuture.completedFuture("hello!")
                .thenApply(s -> s + "world!");
        System.out.println("第一次链式调用返回" + future.get());
        future.thenApply(s -> s + "nice!");//这一次调用将被忽略
        System.out.println("第二次链式调用返回" + future.get());

在这里插入图片描述

还可以进行 流式调用

public static void test3() throws ExecutionException, InterruptedException {
    CompletableFuture<String> future = CompletableFuture.completedFuture("hello!")
            .thenApply(s -> s + "world!").thenApply(s -> s + "nice");
    System.out.println("第一次链式调用返回" + future.get());
}
实战模拟

img

假设你想先获取用户信息后,再并行获取商品详情和物品信息,再获取商品推荐使用CompletableFuture怎么实现呢?

import java.util.concurrent.CompletableFuture;

public class ProductRecommendationExample {

    // 模拟获取用户信息的异步操作
    public static CompletableFuture<User> getUserInfoAsync(int userId) {
        return CompletableFuture.supplyAsync(() -> {
            // 实际获取用户信息的逻辑
            return new User(userId, "User" + userId);
        });
    }

    // 模拟商品详情查询的异步操作
    public static CompletableFuture<ProductDetail> getProductDetailAsync(int productId) {
        return CompletableFuture.supplyAsync(() -> {
            // 实际商品详情查询的逻辑
            return new ProductDetail(productId, "Product" + productId + " Details");
        });
    }

    // 模拟物流信息查询的异步操作
    public static CompletableFuture<ShippingInfo> getShippingInfoAsync(int productId) {
        return CompletableFuture.supplyAsync(() -> {
            // 实际物流信息查询的逻辑
            return new ShippingInfo(productId, "Shipping Info for Product" + productId);
        });
    }

    // 模拟商品推荐的异步操作
    public static CompletableFuture<String> recommendProductAsync(ProductDetail productDetail, ShippingInfo shippingInfo) {
        // 实际商品推荐逻辑,这里简单示范,可以根据具体业务需求进行修改
        return CompletableFuture.supplyAsync(() ->
                "Recommended Product: " + productDetail.getName() + ", " +
                        "Shipping Info: " + shippingInfo.getInfo()
        );
    }

    public static void main(String[] args) {
        int userId = 123;
        int productId = 456;

        // 串行获取用户信息
        CompletableFuture<User> userInfoFuture = getUserInfoAsync(userId);

        // 并行查询商品详情和物流信息,并在两者都完成后进行商品推荐
        CompletableFuture<String> recommendationFuture = userInfoFuture.thenCompose(user ->
                CompletableFuture.allOf(
                        getProductDetailAsync(productId),
                        getShippingInfoAsync(productId)
                ).thenApplyAsync(ignored ->
                        recommendProductAsync(productDetail, shippingInfo).join()
                )
        );

        // 在商品推荐完成后的处理
        recommendationFuture.thenAccept(recommendation ->
                System.out.println("Product Recommendation: " + recommendation));
    }
}

class User {
    private final int userId;
    private final String userName;

    public User(int userId, String userName) {
        this.userId = userId;
        this.userName = userName;
    }

    public int getUserId() {
        return userId;
    }

    public String getUserName() {
        return userName;
    }
}

class ProductDetail {
    private final int productId;
    private final String name;

    public ProductDetail(int productId, String name) {
        this.productId = productId;
        this.name = name;
    }

    public int getProductId() {
        return productId;
    }

    public String getName() {
        return name;
    }
}

class ShippingInfo {
    private final int productId;
    private final String info;

    public ShippingInfo(int productId, String info) {
        this.productId = productId;
        this.info = info;
    }

    public int getProductId() {
        return productId;
    }

    public String getInfo() {
        return info;
    }
}

首先通过 getUserInfoAsync 方法串行获取用户信息。然后,使用 thenCompose 方法,通过 CompletableFuture.allOf 并行执行商品详情查询和物流信息查询,并在两者都完成后使用 thenApplyAsync 方法进行商品推荐。最终,通过 thenAccept 方法在商品推荐完成后进行处理。这样的设计能够充分利用异步操作,提高系统的并发性能

总结

CompletableFuture 提供了强大而灵活的异步编程工具,使得在Java中进行异步操作变得更加简单。通过合理的使用,可以提高应用程序的性能和并发能力。以上只是 CompletableFuture 的一小部分功能,更多功能可以根据实际需求去探索。

希望这篇文章能够帮助你更好地理解和使用 CompletableFuture。 Happy coding! 🚀。点个关注,关注更多干货😘

参考链接:https://javaguide.cn/java/concurrent/completablefuture-intro.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值