Is it safe / good practice to "reuse" CompletedFuture

When experimenting with CompletableFutureme, I was wondering if this code is safe.

    CompletableFuture<Integer> foo = CompletableFuture.supplyAsync(() -> 42);

    foo.thenApply((bar) -> {
        System.out.println("bar " + bar);
        return bar;
    })
    .acceptEither(foo.thenApply((baz) -> {
        System.out.println("baz " + baz);
        return baz;
    }),
    (z) -> System.out.println("finished processing of " + z));

Works, print

bar 42
baz 42
finished processing of 42

Is it a good / good idea to call thenApplyother methods more than once on a given instance CompletableFuture?

+4
source share
1 answer

What you are describing is not "reuse" CompleteableFuture, as it performs only one action and ends no more than once.

, . , . CompletionStage<T>:

, , CompletionStage. , .

" ", " ". , . CompletableFuture<T>.

, a b, , c, , , - c, , c → a → b c → b → a, a b, API concurrency.

, , . "bar" "baz" , 42, , " ", , . , "bar" "baz" "bar, , baz" "baz, , ".

+6

Source: https://habr.com/ru/post/1651409/


All Articles