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

在Java中,CompletableFuture 提供了一种非阻塞、异步编程的方式,特别适合处理多个依赖或并行任务。通过链式调用,可以将多个异步操作串联起来,清晰地表达任务之间的依赖关系,而无需嵌套回调。
当你需要对上一个异步任务的结果进行处理并返回新值时,使用 thenApply。它接收一个函数,该函数的输入是前一个阶段的结果,返回新的结果。
CompletableFuture
.supplyAsync(() -> "Hello")
.thenApply(s -> s + " World");
future.thenAccept(System.out::println); // 输出: Hello World
当你的转换逻辑本身也返回一个 CompletableFuture,应使用 thenCompose。它可以把两个有依赖关系的异步操作串成一条链,避免嵌套 CompletableFuture。
CompletableFuture
.supplyAsync(() -> "User123")
.thenCompose(userId -> fetchUserInfoAsync(userId));
future.thenAccept(info -> System.out.println("User Info: " + info));
其中 fetchUserInfoAsync(String userId) 返回的是 CompletableFuture<string></string>。
立即学习“Java免费学习笔记(深入)”;
如果你有两个不互相依赖的异步任务,但想在它们都完成后合并结果,使用 thenCombine。
CompletableFuture
CompletableFuture
CompletableFuture
combined.thenAccept(System.out::println); // 输出: Hello Java
链式调用中可能发生异常,使用 handle 可以同时处理正常结果和异常,或者用 exceptionally 专门提供降级或默认值。
CompletableFuture
.supplyAsync(() -> {
if (true) throw new RuntimeException("Error!");
return "Success";
})
.exceptionally(ex -> "Fallback Value");
future.thenAccept(System.out::println); // 输出: Fallback Value
以上就是如何在Java中使用CompletableFuture链式调用的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号