Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 31515d0

Browse filesBrowse files
committed
👌 add completablefuture readme
1 parent 7a2be2f commit 31515d0
Copy full SHA for 31515d0

File tree

Expand file treeCollapse file tree

4 files changed

+85
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+85
-1
lines changed
Open diff view settings
Collapse file

‎java8-completablefuture/README.md‎

Copy file name to clipboard
+69Lines changed: 69 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
Collapse file

‎java8-completablefuture/pom.xml‎

Copy file name to clipboard
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>learn-java8</artifactId>
7+
<groupId>io.github.biezhi</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>java8-completablefuture</artifactId>
13+
14+
</project>
Collapse file

‎java8-lambda/src/main/java/io/github/biezhi/java8/lambda/lesson2/FunctionalDemo.java‎

Copy file name to clipboardExpand all lines: java8-lambda/src/main/java/io/github/biezhi/java8/lambda/lesson2/FunctionalDemo.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void consumer() {
3636
*/
3737
public void function() {
3838
Function<String, String> toUpperCase = name -> name.toUpperCase();
39-
toUpperCase.apply("java"); // Java
39+
toUpperCase.apply("Java"); // Java
4040
}
4141

4242
/**
Collapse file

‎pom.xml‎

Copy file name to clipboardExpand all lines: pom.xml
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<module>java8-proper</module>
2121
<module>java8-concurrent</module>
2222
<module>java8-growing</module>
23+
<module>java8-completablefuture</module>
2324
</modules>
2425

2526
<dependencies>

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.