What is the correct way to create an already completed CompletableFuture <Void>

I use full-featured futures in java 8, and I want to write a method that, based on the received parameter, launches several tasks with parallel side effects, and then returns their β€œcombined” future (using CompletableFuture.allOf()), or does nothing and returns the already completed future.

However, allOfreturns a CompletableFuture<Void>:

public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs)

And the only way to create an already completed future that knows, uses completedFuture(), that expects value:

public static <U> CompletableFuture<U> completedFuture(U value)

Returns a new CompletableFuture that is already completed with the given value.

and is Voidnot irreversible, so I need another way to create an already completed future type CompletableFuture<Void>.

?

+4
2

Void , CompletableFuture<Void> null, join() , allOf() .

,

CompletableFuture<Void> cf = CompletableFuture.completedFuture(null);

.

CompletableFuture<Void> cf = CompletableFuture.allOf();

, , . .

+4

a null :

CompletableFuture<Void> done = CompletableFuture.completedFuture(null);
+8

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


All Articles