1+ # CompletableFuture
12
3+ ## 为什么要引入 CompletableFuture
4+
5+ - Future.get() 方法会阻塞线程,一直阻塞,直到其所对应的任务完成或因为异常退出。在不恰当的地方调用此方法会因为线程阻塞而降低系统响应度。就像文中提到的。
6+ - Future.get(long, TimeUnit) 可以一定的时间内超时退出,而不会像前一个方法那样一直阻塞线程。但是这对系统响应性的改进是治标不治本。
7+ - Java 8 引入了 CompletableFuture,可用通过 CompletableFuture<Void > thenAccept(Consumer<? super T> action) 方法异步触发 send(Response) 方法。这时 serve() 方法会很快结束。而当 responseFuture 所对应的任务完成时,send(Response) 方法便会被调用,其执行线程同 responseFuture 所对应的任务的执行线程相同。
8+ - 对于上一点,有人会问,何必这么麻烦?直接在 responseFuture 所对应任务 (Runnable or Callable) 里面调用 send(Response) 方法。各种在实际工作中可能会遇到的问题暂且不说。只说两点:
9+ 1 . responseFuture 所对应任务是不可修改的,比如调用自第三方模块
10+ 2 . 即便代码可修改,但在 responseFuture 所对应的任务中去调用 send(Response) 方法表明的含义是后者的功能从属于前者。这可能从业务角度上看是不合理的。即在本例中,responseFuture 所对应的任务和 send(Response) 方法在业务角度讲是属于同一级的。违反这一点会对代码可读性和可维护性不利。
211
312## 创建 CompletableFuture
413
@@ -24,29 +33,64 @@ CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> a
2433CompletableFuture<T> exceptionally(Function<Throwable,? extends T> fn)
2534```
2635
27- ## 结果转换
36+ 同时进行计算和转换
37+
38+ ``` java
39+ < U > CompletableFuture<U > handle(BiFunction<? super T ,Throwable ,? extends U > fn)
40+ < U > CompletableFuture<U > handleAsync(BiFunction<? super T ,Throwable ,? extends U > fn)
41+ < U > CompletableFuture<U > handleAsync(BiFunction<? super T ,Throwable ,? extends U > fn, Executor executor)
42+ ```
43+
44+ ## 结果转换(map)
2845
2946``` java
3047< U > CompletableFuture<U > thenApply(Function<? super T ,? extends U > fn)
3148< U > CompletableFuture<U > thenApplyAsync(Function<? super T ,? extends U > fn)
3249< U > CompletableFuture<U > thenApplyAsync(Function<? super T ,? extends U > fn, Executor executor)
3350```
3451
35- ## 消耗型
52+ ## flatMap
3653
54+ ``` java
55+ < U > CompletableFuture<U > thenCompose(Function<? super T ,? extends CompletionStage<U > > fn)
56+ < U > CompletableFuture<U > thenComposeAsync(Function<? super T ,? extends CompletionStage<U > > fn)
57+ < U > CompletableFuture<U > thenComposeAsync(Function<? super T ,? extends CompletionStage<U > > fn, Executor executor)
58+ ```
59+
60+ ## 消耗型
3761
3862``` java
3963CompletableFuture<Void > thenAccept(Consumer<? super T > action)
4064CompletableFuture<Void > thenAcceptAsync(Consumer<? super T > action)
4165CompletableFuture<Void > thenAcceptAsync(Consumer<? super T > action, Executor executor)
4266```
4367
68+ ` thenAccept(Consumer<? super T> action) ` 这个方法的命名采用了类似 Promise 的命名风格。
69+ 如果把这个方法命名为 addListener 会更容易理解,但是命名为 addListener 不能体现出 thenAccept 能返回 CompletableFuture 从而形成链式调用的特点。
70+
71+ 当两个 ` CompletionStage ` 都正常完成计算的时候,执行一个 ` Runnable `
72+
73+ ``` java
74+ < U > CompletableFuture<Void > thenAcceptBoth(CompletionStage<? extends U > other, BiConsumer<? super T ,? super U > action)
75+ < U > CompletableFuture<Void > thenAcceptBothAsync(CompletionStage<? extends U > other, BiConsumer<? super T ,? super U > action)
76+ < U > CompletableFuture<Void > thenAcceptBothAsync(CompletionStage<? extends U > other, BiConsumer<? super T ,? super U > action, Executor executor)
77+ CompletableFuture<Void > runAfterBoth(CompletionStage<?> other, Runnable action)
78+ ```
79+
80+ 对上一步的计算结果不关心,执行下一个操作
81+
82+ ``` java
83+ CompletableFuture<Void > thenRun(Runnable action)
84+ CompletableFuture<Void > thenRunAsync(Runnable action)
85+ CompletableFuture<Void > thenRunAsync(Runnable action, Executor executor)
86+ ```
87+
4488## 组合
4589
4690``` 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)
91+ < U , V > CompletableFuture<V > thenCombine( CompletionStage <? extends U > other, BiFunction<? super T ,? super U ,? extends V > fn)
92+ < U , V > CompletableFuture<V > thenCombineAsync( CompletionStage <? extends U > other, BiFunction<? super T ,? super U ,? extends V > fn)
93+ < U , V > CompletableFuture<V > thenCombineAsync( CompletionStage <? extends U > other, BiFunction<? super T ,? super U ,? extends V > fn, Executor executor)
5094```
5195
5296## Either
@@ -67,3 +111,4 @@ static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs)
67111static CompletableFuture<Object > anyOf(CompletableFuture<?> ... cfs)
68112```
69113
114+ [ CompletableFuture 的 20 个例子] ( https://zhuanlan.zhihu.com/p/34921166 )
0 commit comments