What is CompletableFuture.allOf() in Java?
Overview
allOf() is a static method of the CompletableFuture class. It
returns a new CompletableFuture object when all of the specified CompletableFutures are complete.
If any of the specified CompletableFutures are complete with an exception, the resulting CompletableFuture does as well, with a CompletionException as the cause. Otherwise, the outcomes of the specified CompletableFutures are not represented in the returned CompletableFuture, but they may be acquired separately by examining them.
The allOf method is defined in the CompletableFuture class. The CompletableFuture class is defined in the java.util.concurrent package. To import the CompletableFuture class use the following import statement.
import java.util.concurrent.CompletableFuture;
Syntax
public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs)
Parameters
CompletableFuture<?>... cfs: This is the list of completable futures to complete.
Return value
This method returns a new CompletableFuture.
Code
import java.util.Arrays;import java.util.List;import java.util.concurrent.*;import java.util.stream.Collectors;public class Main {static void sleep(int millis){try {Thread.sleep(millis);} catch (InterruptedException e) {e.printStackTrace();}}static void executionThread(){System.out.println("Thread execution - " + Thread.currentThread().getName());}public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<String> completableFuture1 = CompletableFuture.supplyAsync(() -> {sleep(1000);String stringToPrint = "Educative";System.out.println("----\nsupplyAsync first future - " + stringToPrint);executionThread();return stringToPrint;});CompletableFuture<String> completableFuture2 = CompletableFuture.supplyAsync(() -> {sleep(2000);String stringToPrint = "Edpresso";System.out.println("----\nsupplyAsync second future - " + stringToPrint);executionThread();return stringToPrint;});List<CompletableFuture<String>> completableFutures = Arrays.asList(completableFuture1, completableFuture2);CompletableFuture<Void> resultantCf = CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[completableFutures.size()]));CompletableFuture<List<String>> allFutureResults = resultantCf.thenApply(t -> completableFutures.stream().map(CompletableFuture::join).collect(Collectors.toList()));System.out.println("Result - " + allFutureResults.get());sleep(1000);}}
Explanation
- Lines 1 to 4: We import the relevant packages and classes.
- Lines 8 to 14: We define a function called
sleep()that makes the current thread sleep for the given amount of milliseconds. - Lines 16 to 18: We define a function called
executionThread()that prints the current thread of execution. - Lines 22 to 28: We create a completable future called
completableFuture1using thesupplyAsyc()method by passing a supplier that sleeps for 1 second, invokes theexecutionThread()method, and returns a string value. - Lines 30 to 36: We create another completable future called
completableFuture2using thesupplyAsyc()method by passing a supplier that sleeps for 2 seconds, invokes theexecutionThread()method, and returns a string value. - Line 38: We create a list of completable future called
completableFuturesfromcompletableFuture1andcompletableFuture2. - Line 40: We pass the
completableFuturesto theallOf()method. Here, a new future is returned calledresultantCf. - Line 42: Once all the passed futures are completed,
resultantCfwill be in the completed stage. OnceresultantCfis completed stage, we collect the results of the individual futures as a list by using thejoin()method on each future. - Line 44: We print the results of the individual futures.
- Line 45: The main thread sleeps for 1 second.