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;
public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs)
CompletableFuture<?>... cfs
: This is the list of completable futures to complete.This method returns a new CompletableFuture
.
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);}}
sleep()
that makes the current thread sleep for the given amount of milliseconds.executionThread()
that prints the current thread of execution.completableFuture1
using the supplyAsyc()
method by passing a supplier that sleeps for 1 second, invokes the executionThread()
method, and returns a string value.completableFuture2
using the supplyAsyc()
method by passing a supplier that sleeps for 2 seconds, invokes the executionThread()
method, and returns a string value.completableFutures
from completableFuture1
and completableFuture2
.completableFutures
to the allOf()
method. Here, a new future is returned called resultantCf
.resultantCf
will be in the completed stage. Once resultantCf
is completed stage, we collect the results of the individual futures as a list by using the join()
method on each future.