Skip to content

Commit f0683e5

Browse files
committed
[feat]CompletableFuture入门
1 parent ce6daf1 commit f0683e5

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

docs/java/multi-thread/CompletableFuture从入门到实战.md renamed to docs/java/multi-thread/CompletableFuture入门.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,31 @@
44

55
## 简单介绍
66

7-
`CompletableFuture` 同时实现了 `Future``CompletionStage` 接口。其除了提供了更为好用和强大的 `Future` 特性之外,还提供了函数式编程的能力。
7+
`CompletableFuture` 同时实现了 `Future``CompletionStage` 接口。
88

99
```java
1010
public class CompletableFuture<T> implements Future<T>, CompletionStage<T> {
1111
}
1212
```
1313

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+
1432
## 常见操作
1533

1634
### 创建 CompletableFuture
@@ -490,3 +508,11 @@ efg
490508
future1 done...
491509
abc
492510
```
511+
512+
## 后记
513+
514+
这篇文章只是简单介绍了 `CompletableFuture` 比较常用的一些 API 。
515+
516+
如果想要深入学习的话,可以多找一些书籍和博客看。
517+
518+
另外,建议G友们可以看看京东的 [asyncTool](https://gitee.com/jd-platform-opensource/asyncTool) 这个并发框架,里面大量使用到了 `CompletableFuture`

0 commit comments

Comments
 (0)