|
| 1 | + |
| 2 | + |
| 3 | +## 创建 CompletableFuture |
| 4 | + |
| 5 | +以下四个静态方法用来为一段异步执行的代码创建 `CompletableFuture` 对象: |
| 6 | + |
| 7 | +```java |
| 8 | +static CompletableFuture<Void> runAsync(Runnable runnable) |
| 9 | +static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor) |
| 10 | +static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) |
| 11 | +static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) |
| 12 | +``` |
| 13 | + |
| 14 | +以 `Async` 结尾并且没有指定 `Executor` 的方法会使用 `ForkJoinPool.commonPool()` 作为它的线程池执行异步代码。 |
| 15 | + |
| 16 | +## 计算结果完成时的处理 |
| 17 | + |
| 18 | +当 `CompletableFuture` 的计算结果完成,或者抛出异常的时候,我们可以执行特定的 `Action`。 |
| 19 | + |
| 20 | +```javap |
| 21 | +CompletableFuture<T> whenComplete(BiConsumer<? super T,? super Throwable> action) |
| 22 | +CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action) |
| 23 | +CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action, Executor executor) |
| 24 | +CompletableFuture<T> exceptionally(Function<Throwable,? extends T> fn) |
| 25 | +``` |
| 26 | + |
| 27 | +## 结果转换 |
| 28 | + |
| 29 | +```java |
| 30 | +<U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn) |
| 31 | +<U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn) |
| 32 | +<U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor) |
| 33 | +``` |
| 34 | + |
| 35 | +## 消耗型 |
| 36 | + |
| 37 | + |
| 38 | +```java |
| 39 | +CompletableFuture<Void> thenAccept(Consumer<? super T> action) |
| 40 | +CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action) |
| 41 | +CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor) |
| 42 | +``` |
| 43 | + |
| 44 | +## 组合 |
| 45 | + |
| 46 | +```java |
| 47 | +<U> CompletableFuture<U> thenCompose(Function<? super T,? extends CompletionStage<U>> fn) |
| 48 | +<U> CompletableFuture<U> thenComposeAsync(Function<? super T,? extends CompletionStage<U>> fn) |
| 49 | +<U> CompletableFuture<U> thenComposeAsync(Function<? super T,? extends CompletionStage<U>> fn, Executor executor) |
| 50 | +``` |
| 51 | + |
| 52 | +## Either |
| 53 | + |
| 54 | +```java |
| 55 | + CompletableFuture<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action) |
| 56 | + CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action) |
| 57 | + CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor) |
| 58 | +<U> CompletableFuture<U> applyToEither(CompletionStage<? extends T> other, Function<? super T,U> fn) |
| 59 | +<U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T,U> fn) |
| 60 | +<U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T,U> fn, Executor executor) |
| 61 | +``` |
| 62 | + |
| 63 | +## allOf、anyOf |
| 64 | + |
| 65 | +```java |
| 66 | +static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) |
| 67 | +static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) |
| 68 | +``` |
| 69 | + |
0 commit comments