Mateusz Niedbal
Mateusz Niedbal

Reputation: 346

CompletableFuture.allOf meaning

Do I understand it well, that when I use

CompletableFuture.allOf("array of CompletableFuture")
                 .runAsync(()-> { "piece of code" });

first I have to wait until the array of CF are all done , and than the Runnable "piece of code" is done?

Upvotes: 0

Views: 1263

Answers (2)

Eugene
Eugene

Reputation: 120858

That is explicit in the documentation:

Returns a new CompletableFuture that is completed when all of the given CompletableFutures complet

Upvotes: 2

Simon Martinelli
Simon Martinelli

Reputation: 36143

The CompletableFuture.allOf static method allows to wait for completion of all of the Futures provided as a var-arg.

For example

CompletableFuture<String> future1  
  = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2  
  = CompletableFuture.supplyAsync(() -> "Beautiful");
CompletableFuture<String> future3  
  = CompletableFuture.supplyAsync(() -> "World");

CompletableFuture<Void> combinedFuture 
  = CompletableFuture.allOf(future1, future2, future3);

Upvotes: 2

Related Questions