Skip to content

Commit

Permalink
👌 add completablefuture readme
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Mar 25, 2018
1 parent 7a2be2f commit 31515d0
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
69 changes: 69 additions & 0 deletions java8-completablefuture/README.md
@@ -0,0 +1,69 @@


## 创建 CompletableFuture

以下四个静态方法用来为一段异步执行的代码创建 `CompletableFuture` 对象:

```java
static CompletableFuture<Void> runAsync(Runnable runnable)
static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor)
static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)
```

`Async` 结尾并且没有指定 `Executor` 的方法会使用 `ForkJoinPool.commonPool()` 作为它的线程池执行异步代码。

## 计算结果完成时的处理

`CompletableFuture` 的计算结果完成,或者抛出异常的时候,我们可以执行特定的 `Action`

```javap
CompletableFuture<T> whenComplete(BiConsumer<? super T,? super Throwable> action)
CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action)
CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action, Executor executor)
CompletableFuture<T> exceptionally(Function<Throwable,? extends T> fn)
```

## 结果转换

```java
<U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn)
<U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn)
<U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor)
```

## 消耗型


```java
CompletableFuture<Void> thenAccept(Consumer<? super T> action)
CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action)
CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor)
```

## 组合

```java
<U> CompletableFuture<U> thenCompose(Function<? super T,? extends CompletionStage<U>> fn)
<U> CompletableFuture<U> thenComposeAsync(Function<? super T,? extends CompletionStage<U>> fn)
<U> CompletableFuture<U> thenComposeAsync(Function<? super T,? extends CompletionStage<U>> fn, Executor executor)
```

## Either

```java
CompletableFuture<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action)
CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action)
CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor)
<U> CompletableFuture<U> applyToEither(CompletionStage<? extends T> other, Function<? super T,U> fn)
<U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T,U> fn)
<U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T,U> fn, Executor executor)
```

## allOf、anyOf

```java
static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs)
static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs)
```

14 changes: 14 additions & 0 deletions java8-completablefuture/pom.xml
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>learn-java8</artifactId>
<groupId>io.github.biezhi</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>java8-completablefuture</artifactId>

</project>
Expand Up @@ -36,7 +36,7 @@ public void consumer() {
*/
public void function() {
Function<String, String> toUpperCase = name -> name.toUpperCase();
toUpperCase.apply("java"); // Java
toUpperCase.apply("Java"); // Java
}

/**
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -20,6 +20,7 @@
<module>java8-proper</module>
<module>java8-concurrent</module>
<module>java8-growing</module>
<module>java8-completablefuture</module>
</modules>

<dependencies>
Expand Down

0 comments on commit 31515d0

Please sign in to comment.