CompletableFuture 是Java 8中的一个很重要的类,它是一个实现了 Future 接口的异步处理类,可以帮助我们更加方便地执行异步任务和处理异步任务的结果。 CompletableFuture 的底层实现是基于 Fork/Join 框架和 ConcurrentLinkedQueue ,在Java 8中, CompletableFuture 是在 java.util.concurrent 包下。
CompletableFuture 底层依赖了 Fork/Join 框架和 ConcurrentLinkedQueue 。 Fork/Join 框架是Java 7加入的一个框架,它的基本思想是将一个大任务拆成多个小任务并行执行,最后将结果合并起来。而 ConcurrentLinkedQueue 是Java 5中添加的一个线程安全的队列,它支持高并发下的并发读写。
CompletableFuture 底层的异步处理也是基于这两个框架的。异步任务的执行,是将任务拆分成多个小任务,然后并行执行,最后合并结果。同时,异步任务的结果可以是不同类型的对象,如 void 、 value 和 exception 等。
CompletableFuture 类中提供了很多API,常见的有以下几个:
我们可以通过以下两种方式创建CompletableFuture:
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");
CompletableFuture<String> future2 = CompletableFuture.completedFuture("Hello World");如果需要无返回值的CompletableFuture,可以通过以下方式:
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
// 做一些不需要返回值的操作
});在这个示例中,我们创建了一个 CompletableFuture 对象,它使用 runAsync() 方法执行了一个不需要返回值的操作。注意,这个操作是在新的线程中执行的,因此不会阻塞主线程。
可以通过调用 future.join() 来等待这个异步任务的完成。如果不需要等待任务的完成,可以直接返回 future 对象。
通过 supplyAsync 方式创建的CompletableFuture,会执行一个异步任务,并返回结果。而 completedFuture 方式创建的CompletableFuture,则直接返回一个已经完成的CompletableFuture对象。
CompletableFuture 中有很多方法可以用来描述异步任务之间的关系,如:
这些方法的使用方式相似,都是通过链式调用的方式,如下示例:
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");
CompletableFuture<String> future2 = future.thenApply(str -> str + "Java");
CompletableFuture<Void> future3 = future.thenAccept(str -> System.out.println(str));
CompletableFuture<Void> future4 = future.thenRun(() -> System.out.println("Hello Java"));
CompletableFuture<String> future5 = future.thenCompose(str -> CompletableFuture.completedFuture(str + "Java"));
CompletableFuture<String> future6 = future.thenCombine(future5, (str1, str2) -> str1 + str2);
CompletableFuture<Void> future7 = future.whenComplete((str, ex) -> {
if (ex != null) {
System.out.println("Error: " + ex.getMessage());
} else {
System.out.println(str);
}
});thenApply 方法会在 future 任务完成后,将其结果作为参数传递给 thenApply 中的函数,返回另一个 CompletableFuture 。thenAccept 方法会在 future 任务完成后,将其结果作为参数传递给 thenAccept 中的函数,不返回任何结果。thenRun 方法会在 future 任务完成后,执行 thenRun 中的代码,不接受任何参数,也不返回任何结果。thenCompose 方法会在 future 任务完成后,将其结果作为参数传递给 thenCompose 中的 CompletableFuture ,返回另一个 CompletableFuture 。thenCombine 方法会在 future 和 future5 任务都完成后,将两个任务的结果作为参数传递给 thenCombine 中的函数,返回另一个 CompletableFuture 。whenComplete 方法会在 future 任务完成后,执行 whenComplete 中的代码,无论成功或失败都会执行。CompletableFuture 中有很多方法可以用来处理异步任务中出现的异常,如:
这些方法的使用方式相似,都是通过链式调用的方式,如下示例:
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
if (System.currentTimeMillis() % 2 == 0) {
throw new RuntimeException();
}
return "Hello World";
});
CompletableFuture<String> future2 = future.exceptionally(ex -> "Error")
.thenApply(str -> str + " Java");
CompletableFuture<String> future3 = future.handle((str, ex) -> {
if (ex != null) {
System.out.println("Error: " + ex.getMessage());
return "Error";
} else {
return str;
}
});exceptionally 方法会在 future 任务出现异常的时候执行,并返回一个带有默认值的 CompletableFuture 。handle 方法会在 future 任务完成后,无论成功或失败都会执行,并可以处理异常信息。CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");通过 supplyAsync 创建一个异步执行任务,返回值为 Hello World 。
thenApply 示例CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");
CompletableFuture<String> future2 = future.thenApply(str -> str + " Java");
System.out.println(future2.get());输出结果为 Hello World Java 。
thenAccept 示例CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");
CompletableFuture<Void> future2 = future.thenAccept(str -> System.out.println(str + " Java"));
future2.get();输出结果为 Hello World Java 。
thenRun 示例CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");
CompletableFuture<Void> future2 = future.thenRun(() -> System.out.println("Hello Java"));
future2.get();输出结果为 Hello Java 。
thenCompose 示例CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");
CompletableFuture<String> future2 = future.thenCompose(str -> CompletableFuture.completedFuture(str + " Java"));
System.out.println(future2.get());输出结果为 Hello World Java 。
thenCombine 示例CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> " World");
CompletableFuture<String> future3 = future.thenCombine(future2, (str1, str2) -> str1 + str2);
System.out.println(future3.get());输出结果为 Hello World 。
whenComplete 示例CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");
CompletableFuture<Void> future2 = future.whenComplete((str, ex) -> {
if (ex != null) {
System.out.println("Error: " + ex.getMessage());
} else {
System.out.println(str);
}
});
future2.get();输出结果为 Hello World 。
exceptionally 示例CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
if (System.currentTimeMillis() % 2 == 0) {
throw new RuntimeException();
}
return "Hello World";
});
CompletableFuture<String> future2 = future.exceptionally(ex -> "Error").thenApply(str -> str + " Java");
System.out.println(future2.get());输出结果为 Error Java 。
handle 示例CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
if (System.currentTimeMillis() % 2 == 0) {
throw new RuntimeException();
}
return "Hello World";
});
CompletableFuture<String> future2 = future.handle((str, ex) -> {
if (ex != null) {
System.out.println("Error: " + ex.getMessage());
return "Error";
} else {
return str;
}
});
System.out.println(future2.get());输出结果为 Error 或 Hello World 。