File tree Expand file tree Collapse file tree 1 file changed +27
-1
lines changed Expand file tree Collapse file tree 1 file changed +27
-1
lines changed Original file line number Diff line number Diff line change 4
4
5
5
## 简单介绍
6
6
7
- ` CompletableFuture ` 同时实现了 ` Future ` 和 ` CompletionStage ` 接口。其除了提供了更为好用和强大的 ` Future ` 特性之外,还提供了函数式编程的能力。
7
+ ` CompletableFuture ` 同时实现了 ` Future ` 和 ` CompletionStage ` 接口。
8
8
9
9
``` java
10
10
public class CompletableFuture <T> implements Future<T > , CompletionStage<T > {
11
11
}
12
12
```
13
13
14
+ ` CompletableFuture ` 除了提供了更为好用和强大的 ` Future ` 特性之外,还提供了函数式编程的能力。
15
+
16
+ ![ ] ( https://guide-blog-images.oss-cn-shenzhen.aliyuncs.com/javaguide/image-20210902092441434.png )
17
+
18
+ ` Future ` 接口有 5 个方法:
19
+
20
+ - ` boolean cancel(boolean mayInterruptIfRunning) ` :尝试取消执行任务。
21
+ - ` boolean isCancelled() ` :判断任务是否被取消。
22
+ - ` boolean isDone() ` : 判断任务是否已经被执行完成。
23
+ - ` get() ` :等待任务执行完成并获取运算结果。
24
+ - ` get(long timeout, TimeUnit unit) ` :多了一个超时时间。
25
+
26
+ ![ ] ( https://guide-blog-images.oss-cn-shenzhen.aliyuncs.com/javaguide/image-20210902093026059.png )
27
+
28
+ ` CompletionStage<T> ` 接口中的方法比较多,` CompletableFuture ` 的函数式能力就是这个接口赋予的。从这个接口的方法参数你就可以发现其大量使用了 Java8 引入的函数式编程。
29
+
30
+ 由于方法众多,所以这里不能一一讲解,下文中我会介绍大部分常见方法的使用。
31
+
14
32
## 常见操作
15
33
16
34
### 创建 CompletableFuture
490
508
future1 done...
491
509
abc
492
510
```
511
+
512
+ ## 后记
513
+
514
+ 这篇文章只是简单介绍了 ` CompletableFuture ` 比较常用的一些 API 。
515
+
516
+ 如果想要深入学习的话,可以多找一些书籍和博客看。
517
+
518
+ 另外,建议G友们可以看看京东的 [ asyncTool] ( https://gitee.com/jd-platform-opensource/asyncTool ) 这个并发框架,里面大量使用到了 ` CompletableFuture ` 。
You can’t perform that action at this time.
0 commit comments