CompletableFuture通过supplyAsync实现异步任务,thenApply进行同步转换,thenCompose串联异步依赖,thenCombine合并并行结果,并结合exceptionally处理异常,构建高效非阻塞异步链。

在Java中,CompletableFuture 是实现异步编程的重要工具,它支持非阻塞的链式调用,能有效提升程序响应性和吞吐量。通过组合多个异步任务,可以构建清晰、可维护的异步流程。
使用 CompletableFuture.supplyAsync() 可以启动一个带返回值的异步任务,通常用于执行耗时操作(如网络请求、数据库查询)而不阻塞主线程。
例如:
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
// 模拟耗时操作
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return "Hello from async";
});
在前一个任务完成后,若需要对结果进行处理并返回新值,可使用 thenApply。它是同步执行的,适用于轻量级计算。
立即学习“Java免费学习笔记(深入)”;
示例:将上一步结果转为大写
CompletableFuture<String> transformed = future.thenApply(result -> result.toUpperCase()); transformed.thenAccept(System.out::println); // 输出: HELLO FROM ASYNC
当下一个任务依赖前一个任务的结果且本身也是异步时,使用 thenCompose。它将两个 CompletableFuture 串联起来,形成真正的异步链。
举例:根据用户ID异步获取用户名,再异步获取其权限
CompletableFuture<String> getUserName(int id) {
return CompletableFuture.supplyAsync(() -> {
// 模拟查询
return "User" + id;
});
}
CompletableFuture<String> getPermissions(String userName) {
return CompletableFuture.supplyAsync(() -> {
return "ROLE_ADMIN";
});
}
// 链式调用
CompletableFuture<String> chain = getUserName(123)
.thenCompose(name -> getPermissions(name));
chain.thenAccept(perm -> System.out.println("Permission: " + perm));
如果两个异步任务无依赖关系,可并行执行,并用 thenCombine 合并结果。
示例:同时获取用户信息和订单数量,最后汇总
CompletableFuture<String> userInfo = CompletableFuture.supplyAsync(() -> "John");
CompletableFuture<Integer> orderCount = CompletableFuture.supplyAsync(() -> 5);
CompletableFuture<String> summary = userInfo.thenCombine(orderCount,
(user, count) -> user + " has " + count + " orders");
summary.thenAccept(System.out::println);
异步链中可能发生异常,需通过 exceptionally 或 handle 进行捕获和恢复。
示例:提供默认值防止链中断
CompletableFuture<String> withError = CompletableFuture.supplyAsync(() -> {
throw new RuntimeException("Oops!");
});
CompletableFuture<String> recovered = withError.exceptionally(ex -> {
System.err.println("Error: " + ex.getMessage());
return "Default User";
});
recovered.thenAccept(System.out::println); // 输出: Default User
基本上就这些。合理使用 supplyAsync、thenApply、thenCompose、thenCombine 和异常处理机制,就能构建健壮的异步调用链。关键是理解每个方法的执行时机和线程行为,避免阻塞或资源浪费。
以上就是在Java中如何使用CompletableFuture进行异步链式调用_异步链式调用实践方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号