diff --git a/java8-completablefuture/README.md b/java8-completablefuture/README.md new file mode 100644 index 0000000..9ce15df --- /dev/null +++ b/java8-completablefuture/README.md @@ -0,0 +1,69 @@ + + +## 创建 CompletableFuture + +以下四个静态方法用来为一段异步执行的代码创建 `CompletableFuture` 对象: + +```java +static CompletableFuture runAsync(Runnable runnable) +static CompletableFuture runAsync(Runnable runnable, Executor executor) +static CompletableFuture supplyAsync(Supplier supplier) +static CompletableFuture supplyAsync(Supplier supplier, Executor executor) +``` + +以 `Async` 结尾并且没有指定 `Executor` 的方法会使用 `ForkJoinPool.commonPool()` 作为它的线程池执行异步代码。 + +## 计算结果完成时的处理 + +当 `CompletableFuture` 的计算结果完成,或者抛出异常的时候,我们可以执行特定的 `Action`。 + +```javap +CompletableFuture whenComplete(BiConsumer action) +CompletableFuture whenCompleteAsync(BiConsumer action) +CompletableFuture whenCompleteAsync(BiConsumer action, Executor executor) +CompletableFuture exceptionally(Function fn) +``` + +## 结果转换 + +```java + CompletableFuture thenApply(Function fn) + CompletableFuture thenApplyAsync(Function fn) + CompletableFuture thenApplyAsync(Function fn, Executor executor) +``` + +## 消耗型 + + +```java +CompletableFuture thenAccept(Consumer action) +CompletableFuture thenAcceptAsync(Consumer action) +CompletableFuture thenAcceptAsync(Consumer action, Executor executor) +``` + +## 组合 + +```java + CompletableFuture thenCompose(Function> fn) + CompletableFuture thenComposeAsync(Function> fn) + CompletableFuture thenComposeAsync(Function> fn, Executor executor) +``` + +## Either + +```java + CompletableFuture acceptEither(CompletionStage other, Consumer action) + CompletableFuture acceptEitherAsync(CompletionStage other, Consumer action) + CompletableFuture acceptEitherAsync(CompletionStage other, Consumer action, Executor executor) + CompletableFuture applyToEither(CompletionStage other, Function fn) + CompletableFuture applyToEitherAsync(CompletionStage other, Function fn) + CompletableFuture applyToEitherAsync(CompletionStage other, Function fn, Executor executor) +``` + +## allOf、anyOf + +```java +static CompletableFuture allOf(CompletableFuture... cfs) +static CompletableFuture anyOf(CompletableFuture... cfs) +``` + diff --git a/java8-completablefuture/pom.xml b/java8-completablefuture/pom.xml new file mode 100644 index 0000000..62b3144 --- /dev/null +++ b/java8-completablefuture/pom.xml @@ -0,0 +1,14 @@ + + + + learn-java8 + io.github.biezhi + 1.0-SNAPSHOT + + 4.0.0 + + java8-completablefuture + + \ No newline at end of file diff --git a/java8-lambda/src/main/java/io/github/biezhi/java8/lambda/lesson2/FunctionalDemo.java b/java8-lambda/src/main/java/io/github/biezhi/java8/lambda/lesson2/FunctionalDemo.java index 211bed1..23e4498 100644 --- a/java8-lambda/src/main/java/io/github/biezhi/java8/lambda/lesson2/FunctionalDemo.java +++ b/java8-lambda/src/main/java/io/github/biezhi/java8/lambda/lesson2/FunctionalDemo.java @@ -36,7 +36,7 @@ public void consumer() { */ public void function() { Function toUpperCase = name -> name.toUpperCase(); - toUpperCase.apply("java"); // Java + toUpperCase.apply("Java"); // Java } /** diff --git a/pom.xml b/pom.xml index dce138b..5c15e63 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,7 @@ java8-proper java8-concurrent java8-growing + java8-completablefuture