Java中的CompletableFuture详解
CompletableFuture
是Java 8引入的一个强大的工具,用于处理异步编程。它实现了 Future
接口,并增加了很多功能,使得处理异步任务更加方便和灵活。本文将详细介绍 CompletableFuture
的使用,包括其基本概念、创建和组合异步任务、处理结果及异常、以及一些高级用法。
1. 基本概念
CompletableFuture
是一个可以手动完成的 Future
。它不仅可以用来表示一个异步计算的结果,还可以将多个异步任务串联起来,形成复杂的异步流程。
2. 创建CompletableFuture
2.1 创建一个已完成的CompletableFuture
CompletableFuture<String> completedFuture = CompletableFuture.completedFuture("Hello");
2.2 异步运行任务
使用 runAsync
和 supplyAsync
方法可以异步地运行任务:
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
// 异步任务
System.out.println("Task is running asynchronously");
});
CompletableFuture<String> futureWithResult = CompletableFuture.supplyAsync(() -> {
// 异步任务并返回结果
return "Result of the asynchronous computation";
});
3. 组合多个CompletableFuture
3.1 thenApply
使用 thenApply
方法处理异步计算的结果,并返回一个新的 CompletableFuture
:
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello")
.thenApply(result -> result + " World");
3.2 thenAccept
使用 thenAccept
方法处理计算的结果,但不返回新的 CompletableFuture
:
CompletableFuture.supplyAsync(() -> "Hello")
.thenAccept(result -> System.out.println(result + " World"));
3.3 thenCombine
使用 thenCombine
方法组合两个独立的 CompletableFuture
,并返回一个新的 CompletableFuture
:
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World");
CompletableFuture<String> combinedFuture = future1.thenCombine(future2, (result1, result2) -> result1 + " " + result2);
combinedFuture.thenAccept(System.out::println);
4. 异常处理
CompletableFuture
提供了多种方法来处理异步任务中的异常。
4.1 exceptionally
使用 exceptionally
方法在计算出现异常时提供默认值:
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
if (true) throw new RuntimeException("Exception occurred");
return "Result";
}).exceptionally(ex -> "Default Result");
4.2 handle
使用 handle
方法处理正常和异常的结果:
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
if (true) throw new RuntimeException("Exception occurred");
return "Result";
}).handle((result, ex) -> {
if (ex != null) {
return "Default Result";
} else {
return result;
}
});
5. 组合异步任务
5.1 allOf
使用 allOf
方法等待所有给定的 CompletableFuture
完成:
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World");
CompletableFuture<Void> allOf = CompletableFuture.allOf(future1, future2);
allOf.thenRun(() -> {
try {
System.out.println(future1.get() + " " + future2.get());
} catch (Exception e) {
e.printStackTrace();
}
});
5.2 anyOf
使用 anyOf
方法只要任意一个 CompletableFuture
完成:
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World");
CompletableFuture<Object> anyOf = CompletableFuture.anyOf(future1, future2);
anyOf.thenAccept(result -> System.out.println(result));
思维导图
- CompletableFuture详解
- 基本概念
- 创建CompletableFuture
- completedFuture
- runAsync和supplyAsync
- 组合多个CompletableFuture
- thenApply
- thenAccept
- thenCombine
- 异常处理
- exceptionally
- handle
- 组合异步任务
- allOf
- anyOf
总结
CompletableFuture
提供了一种简洁而强大的方式来处理Java中的异步编程。通过它,我们可以轻松地创建和组合异步任务,并处理任务中的异常。掌握 CompletableFuture
的使用,将显著提升Java并发编程的效率和代码可读性。