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

在Java中,CompletableFuture 是实现异步编程的重要工具,它支持非阻塞的链式调用,能有效提升程序响应性和吞吐量。通过组合多个异步任务,可以构建清晰、可维护的异步流程。
1. 基本异步任务创建
使用 CompletableFuture.supplyAsync() 可以启动一个带返回值的异步任务,通常用于执行耗时操作(如网络请求、数据库查询)而不阻塞主线程。
例如:
CompletableFuturefuture = CompletableFuture.supplyAsync(() -> { // 模拟耗时操作 try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } return "Hello from async"; });
2. 使用 thenApply 进行链式数据转换
在前一个任务完成后,若需要对结果进行处理并返回新值,可使用 thenApply。它是同步执行的,适用于轻量级计算。
立即学习“Java免费学习笔记(深入)”;
示例:将上一步结果转为大写
CompletableFuturetransformed = future.thenApply(result -> result.toUpperCase()); transformed.thenAccept(System.out::println); // 输出: HELLO FROM ASYNC
3. 使用 thenCompose 实现串行异步依赖
当下一个任务依赖前一个任务的结果且本身也是异步时,使用 thenCompose。它将两个 CompletableFuture 串联起来,形成真正的异步链。
举例:根据用户ID异步获取用户名,再异步获取其权限
CompletableFuturegetUserName(int id) { return CompletableFuture.supplyAsync(() -> { // 模拟查询 return "User" + id; }); } CompletableFuture getPermissions(String userName) { return CompletableFuture.supplyAsync(() -> { return "ROLE_ADMIN"; }); } // 链式调用 CompletableFuture chain = getUserName(123) .thenCompose(name -> getPermissions(name)); chain.thenAccept(perm -> System.out.println("Permission: " + perm));
4. 使用 thenCombine 并行合并两个异步结果
如果两个异步任务无依赖关系,可并行执行,并用 thenCombine 合并结果。
示例:同时获取用户信息和订单数量,最后汇总
CompletableFutureuserInfo = CompletableFuture.supplyAsync(() -> "John"); CompletableFuture orderCount = CompletableFuture.supplyAsync(() -> 5); CompletableFuture summary = userInfo.thenCombine(orderCount, (user, count) -> user + " has " + count + " orders"); summary.thenAccept(System.out::println);
5. 异常处理与默认值设置
异步链中可能发生异常,需通过 exceptionally 或 handle 进行捕获和恢复。
示例:提供默认值防止链中断
CompletableFuturewithError = CompletableFuture.supplyAsync(() -> { throw new RuntimeException("Oops!"); }); CompletableFuture recovered = withError.exceptionally(ex -> { System.err.println("Error: " + ex.getMessage()); return "Default User"; }); recovered.thenAccept(System.out::println); // 输出: Default User
基本上就这些。合理使用 supplyAsync、thenApply、thenCompose、thenCombine 和异常处理机制,就能构建健壮的异步调用链。关键是理解每个方法的执行时机和线程行为,避免阻塞或资源浪费。










