如何在Java中使用CompletableFuture链式调用

CompletableFuture通过thenApply转换结果、thenCompose串联异步任务、thenCombine合并独立结果,并结合exceptionally处理异常,实现清晰的异步编程链。

在Java中,CompletableFuture 提供了一种非阻塞、异步编程的方式,特别适合处理多个依赖或并行任务。通过链式调用,可以将多个异步操作串联起来,清晰地表达任务之间的依赖关系,而无需嵌套回调。

1. 使用 thenApply 进行结果转换

当你需要对上一个异步任务的结果进行处理并返回新值时,使用 thenApply。它接收一个函数,该函数的输入是前一个阶段的结果,返回新的结果。

CompletableFuture future = CompletableFuture
  .supplyAsync(() -> "Hello")
  .thenApply(s -> s + " World");
future.thenAccept(System.out::println); // 输出: Hello World

2. 使用 thenCompose 进行异步任务串联

当你的转换逻辑本身也返回一个 CompletableFuture,应使用 thenCompose。它可以把两个有依赖关系的异步操作串成一条链,避免嵌套 CompletableFuture。

CompletableFuture future = CompletableFuture
  .supplyAsync(() -> "User123")
  .thenCompose(userId -> fetchUserInfoAsync(userId));
future.thenAccept(info -> System.out.println("User Info: " + info));

其中 fetchUserInfoAsync(String userId) 返回的是 CompletableFuture

3. 使用 thenCombine 合并两个独立的异步结果

如果你有两个不互相依赖的异步任务,但想在它们都完成后合并结果,使用 thenCombine

CompletableFuture part1 = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture part2 = CompletableFuture.supplyAsync(() -> "Java");

CompletableFuture combined = part1.thenCombine(part2, (a, b) -> a + " " + b);
combined.thenAccept(System.out::println); // 输出: Hello Java

4. 异常处理:handle 或 exceptionally

链式调用中可能发生异常,使用 handle 可以同时处理正常结果和异常,或者用 exceptionally 专门提供降级或默认值。

CompletableFuture future = CompletableFuture
  .supplyAsync(() -> {
    if (true) throw new RuntimeException("Error!");
    return "Success";
  })
  .exceptionally(ex -> "Fallback Value");
future.thenAccept(System.out::println); // 输出: Fallback Value

基本上就这些。通过组合 supplyAsync、thenApply、thenCompose、thenCombine 和异常处理方法,你可以构建清晰、可读性强的异步操作链。关键是根据任务是否异步返回 CompletableFuture 来选择 thenApply 还是 thenCompose。