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

在Java中,CompletableFuture 是实现异步编程的重要工具,它提供了非阻塞方式执行任务、组合多个异步操作以及处理异常的能力。相比传统的 Future,CompletableFuture 支持函数式编程风格,可以链式调用,更加灵活和强大。
使用 CompletableFuture.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免费学习笔记(深入)”;
CompletableFuture.supplyAsync(() -> "Hello")
.thenApply(s -> s + " World")
.thenAccept(System.out::println)
.thenRun(() -> System.out.println("结束"));
CompletableFuture 支持将多个异步任务组合起来:
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 方法处理:
CompletableFuture.supplyAsync(() -> {
if (true) throw new RuntimeException("出错");
return "正常";
}).exceptionally(ex -> {
System.out.println("错误:" + ex.getMessage());
return "默认值";
}).thenAccept(System.out::println); // 输出:默认值
基本上就这些。合理使用 CompletableFuture 能显著提升程序响应性和吞吐量,特别是在IO密集或远程调用场景中。关键是理解其非阻塞性质,避免在主线程中过早调用 get() 导致阻塞。
以上就是如何在Java中实现CompletableFuture异步任务的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号