CompletableFuture 提供非阻塞异步编程,支持链式调用与任务组合,通过 supplyAsync/runAsync 创建任务,thenApply/thenAccept/thenRun 连接操作,allOf/anyOf 管理多任务,exceptionally/handle 处理异常,避免阻塞可提升 IO 密集场景性能。

在Java中,CompletableFuture 是实现异步编程的重要工具,它提供了非阻塞方式执行任务、组合多个异步操作以及处理异常的能力。相比传统的 Future,CompletableFuture 支持函数式编程风格,可以链式调用,更加灵活和强大。
创建异步任务
使用 CompletableFuture.supplyAsync() 或 runAsync() 可以启动一个异步任务:
- supplyAsync:用于有返回值的异步任务
- runAsync:用于无返回值的任务
默认情况下,这些方法使用 ForkJoinPool.commonPool() 线程池,也可以传入自定义线程池提高控制力。
// 示例:有返回值的异步任务
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
System.out.println("任务正在执行,线程:" + Thread.currentThread().getName());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return "结果完成";
});
// 获取结果(会阻塞)
String result = future.get(); // 输出:结果完成
任务完成后执行操作
可以使用 thenApply、thenAccept、thenRun 在前一个任务完成后执行后续操作:
立即学习“Java免费学习笔记(深入)”;
- thenApply:接收上一步结果并返回新值
- thenAccept:消费结果,不返回值
- thenRun:不接收参数,仅运行任务
CompletableFuture.supplyAsync(() -> "Hello")
.thenApply(s -> s + " World")
.thenAccept(System.out::println)
.thenRun(() -> System.out.println("结束"));
组合多个异步任务
CompletableFuture 支持将多个异步任务组合起来:
- thenCompose:串行组合两个依赖任务(flatMap 风格)
- thenCombine:并行执行两个任务,并合并结果
- allOf:等待所有任务完成
- anyOf:任一任务完成即响应
CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> "A");
CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> "B");
CompletableFuture<Void> combined = f1.thenCombine(f2, (a, b) -> a + b)
.thenAccept(System.out::println); // 输出 AB
异常处理
异步任务中发生异常不会立即抛出,需通过 exceptionally 或 handle 方法处理:
- exceptionally:捕获异常并提供默认值
- handle:无论成功或失败都执行,可用于统一处理
CompletableFuture.supplyAsync(() -> {
if (true) throw new RuntimeException("出错");
return "正常";
}).exceptionally(ex -> {
System.out.println("错误:" + ex.getMessage());
return "默认值";
}).thenAccept(System.out::println); // 输出:默认值
基本上就这些。合理使用 CompletableFuture 能显著提升程序响应性和吞吐量,特别是在IO密集或远程调用场景中。关键是理解其非阻塞性质,避免在主线程中过早调用 get() 导致阻塞。









