CompletableFutureの備忘録

CompletableFutureの使い方をよく忘れるので備忘録。

Memo

staticメソッド

completedFuture

CompletableFuture<U> completedFuture(U value)

CompletableFuture.completedFuture("OK")

supplyAsync

CompletableFuture<U> supplyAsync(Supplier<U> supplier)

CompletableFuture.supplyAsync(() -> "OK")

runAsync

CompletableFuture<Void> runAsync(Runnable runnable)

CompletableFuture.runAsync(() -> System.out.println("OK"))

allOf

CompletableFuture<Void> allOf(CompletableFuture<?>... cfs)
戻り値はVoid。

CompletableFuture.allOf(CompletableFuture.runAsync(() -> System.out.println("OK")),
                        CompletableFuture.runAsync(() -> System.out.println("OK")))

anyOf

CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs)
戻り値を返せる。

CompletableFuture.anyOf(CompletableFuture.completedFuture(1),
                        CompletableFuture.completedFuture(2))
                 .thenAccept(System.out::println);

thenXxxx

thenRun

CompletableFuture<Void> thenRun(Runnable action)
Runnableを実行する。

CompletableFuture.runAsync(() -> System.out.println("OK"))
                 .thenRun(() -> System.out.println("OK"));

thenAccept

CompletableFuture<Void> thenAccept(Consumer<? super T> action)
Consumerを実行する。

CompletableFuture.completedFuture("OK")
                 .thenAccept(System.out::println);

thenApply

CompletableFuture<U> thenApply(Function<? super T,? extends U> fn)
Functionを実行する。

CompletableFuture.completedFuture("OK")
                 .thenApply(value -> value + "OK")
                 .thenAccept(System.out::println);

thenCompose

CompletableFuture<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn)
Functionを実行する。thenApplyとの違いは、CompletionStageを返す。

CompletableFuture.completedFuture("OK")
                 .thenCompose(value -> CompletableFuture.completedFuture(value + "OK"))
                 .thenAccept(System.out::println);

thenCombine

CompletableFuture<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn)
2つの実行結果をBiFunctionで受け取る。

CompletableFuture.completedFuture("OK")
                 .thenCombine(CompletableFuture.completedFuture("OK"),
                              (value1, value2) -> value1 + value2)
                 .thenAccept(System.out::println);

xxxxEither

runAfterEither

CompletableFuture<Void> runAfterEither(CompletionStage<?> other, Runnable action)
どちらかが実行された後で、Runnableを実行する。

CompletableFuture.runAsync(() -> System.out.println(1))
                 .runAfterEither(CompletableFuture.runAsync(() -> System.out.println(2)),
                                 () -> System.out.println("OK"));

acceptEither

CompletableFuture<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action)
どちらかの実行結果をConsumerで受け取る。

CompletableFuture.completedFuture(1)
                 .acceptEither(CompletableFuture.completedFuture(2),
                               System.out::println);

applyToEither

CompletableFuture<U> applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn)
どちらかの実行結果をFunctionで受け取る。

CompletableFuture.completedFuture(1)
                 .applyToEither(CompletableFuture.completedFuture(2),
                                value -> value)
                 .thenAccept(System.out::println);

xxxxBoth

runAfterBoth

CompletableFuture<Void> runAfterBoth(CompletionStage<?> other, Runnable action) 両方が実行された後で、Runnableを実行する。

CompletableFuture.runAsync(() -> System.out.println(1))
                 .runAfterBoth(CompletableFuture.runAsync(() -> System.out.println(2)),
                               () -> System.out.println("OK"));

thenAcceptBoth

CompletableFuture<Void> thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action)
両方の実行結果をBiConsumerで受け取る。

CompletableFuture.completedFuture(1)
                 .thenAcceptBoth(CompletableFuture.completedFuture(2),
                                 (value1, value2) -> System.out.println(value1 + value2));

例外ハンドリング

exceptionally

CompletableFuture<T> exceptionally(Function<Throwable, ? extends T> fn)
例外発生時にのみ呼ばれて、値を返せる。

CompletableFuture.failedFuture(new RuntimeException())
                 .exceptionally(throwable -> "Error")
                 .thenAccept(System.out::println)

handle

CompletableFuture<U> handle(BiFunction<? super T, Throwable, ? extends U> fn)
例外発生時に値を返せる。Tを受け取ってUを返すので、別の型を返すこともできる。

CompletableFuture.failedFuture(new RuntimeException())
                 .handle((value, throwable) -> {
                     if (throwable == null) {
                         return value;
                     } else {
                         return "Error";
                     }
                 })
                 .thenAccept(System.out::println);

whenComplete

CompletableFuture<T> whenComplete(BiConsumer<? super T, ? super Throwable> action)
例外発生時の処理を追加できるが、BiConsumerなので値は返せない。

CompletableFuture.failedFuture(new RuntimeException())
                 .whenComplete((value, throwable) -> {
                     if (throwable == null) {
                         System.out.println(value);
                     } else {
                         System.err.println("Error");
                     }
                 });

参考