CompletableFuture 提供了比传统 Future 更强大的异步编程能力,支持链式调用、任务组合与异常处理;通过 supplyAsync 和 runAsync 可创建异步任务,默认使用公共线程池,也可指定自定义线程池;thenApply、thenAccept、thenRun 实现任务的链式处理;thenCompose 用于串行化依赖任务,thenCombine 合并两个并行任务结果;exceptionally 和 handle 方法用于异常捕获与恢复,提升异步代码的健壮性与可读性。

在Java中,CompletableFuture 是处理异步编程的强大工具。它实现了 Future 和 CompletionStage 接口,支持链式调用、组合多个异步任务,并能更好地处理异常和回调。相比传统的 Future,CompletableFuture 提供了更灵活的非阻塞操作方式。
创建异步任务
你可以使用 CompletableFuture.supplyAsync() 或 runAsync() 来启动一个异步任务:
-
supplyAsync():用于有返回值的任务,返回 CompletableFuture
-
runAsync():用于无返回值的任务,返回 CompletableFuture
示例:
CompletableFuturefuture = CompletableFuture.supplyAsync(() -> { // 模拟耗时操作 try { Thread.sleep(1000); } catch (InterruptedException e) {} return "Hello from async"; }); // 获取结果(不推荐阻塞,仅演示) String result = future.get(); // 输出: Hello from async
默认使用 ForkJoinPool.commonPool() 执行任务,也可以传入自定义线程池:
立即学习“Java免费学习笔记(深入)”;
ExecutorService executor = Executors.newFixedThreadPool(4); CompletableFuturefuture = CompletableFuture.supplyAsync(() -> { return "Task executed in custom thread pool"; }, executor);
链式处理结果(thenApply, thenAccept, thenRun)
CompletableFuture 支持在任务完成后执行后续操作:
- thenApply():接收上一步结果,返回新的值
- thenAccept():接收结果,但不返回值(消费型)
- thenRun():不关心结果,只运行一段逻辑
示例:
CompletableFuture.supplyAsync(() -> "Hello")
.thenApply(s -> s + " World")
.thenAccept(System.out::println) // 输出: Hello World
.thenRun(() -> System.out.println("Done"));
组合多个异步任务
可以将多个异步任务组合起来:
AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术。它不是新的编程语言,而是一种使用现有标准的新方法,最大的优点是在不重新加载整个页面的情况下,可以与服务器交换数据并更新部分网页内容,不需要任何浏览器插件,但需要用户允许JavaScript在浏览器上执行。《php中级教程之ajax技术》带你快速
- thenCompose():用于串行组合两个依赖的异步任务(flatMap 风格)
- thenCombine():并行执行两个任务,并合并结果
示例 - thenCompose(一个任务依赖另一个的结果):
CompletableFuturecombined = CompletableFuture .supplyAsync(() -> "Hello") .thenCompose(s -> CompletableFuture.supplyAsync(() -> s + " Java")); combined.thenAccept(System.out::println); // 输出: Hello Java
示例 - thenCombine(并行执行并合并):
CompletableFuturepart1 = CompletableFuture.supplyAsync(() -> "Hello"); CompletableFuture part2 = CompletableFuture.supplyAsync(() -> "World"); part1.thenCombine(part2, (a, b) -> a + " " + b) .thenAccept(System.out::println); // 输出: Hello World
异常处理
异步任务中发生异常不会自动抛出,需显式处理:
- exceptionally():捕获异常并提供默认值
- handle():无论是否异常都会执行,可统一处理结果或错误
示例:
CompletableFuture.supplyAsync(() -> {
throw new RuntimeException("Oops!");
})
.exceptionally(ex -> {
System.out.println("Error: " + ex.getMessage());
return "Fallback Value";
})
.thenAccept(System.out::println); // 输出: Fallback Value
使用 handle 的方式:
CompletableFuture.supplyAsync(() -> {
if (true) throw new RuntimeException("Boom!");
return "Success";
}).handle((result, throwable) -> {
if (throwable != null) {
return "Handled error: " + throwable.getMessage();
}
return result;
}).thenAccept(System.out::println);
基本上就这些。CompletableFuture 让 Java 的异步编程变得清晰且可控,合理使用可以显著提升程序响应性和吞吐量。









