What is the CompletableFuture.anyOf() method in Java?
Overview
In Java, anyOf() is a static method of the CompletableFuture class. It
returns a new class with the same output as any of the specified classes that got completed first.
The anyOf() method gets tricky when a list of CompletableFuture classes return multiple types of outcomes. Due to this, the user is not able to tell which future got completed first.
The CompletableFuture class is defined in the java.util.concurrent package. The following statement is used to import this class:
import java.util.concurrent.CompletableFuture;
Syntax
public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs)
Parameters
CompletableFuture<?>... cfs: The list of completable futures.
Return value
The anyOf() method returns a new CompletableFuture.
Code
import java.util.Arrays;import java.util.List;import java.util.concurrent.*;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<Object> resultantCf = CompletableFuture.anyOf(completableFutures.toArray(new CompletableFuture[completableFutures.size()]));System.out.println("Final Result - " + resultantCf.get());sleep(1000);}}
Explanation
-
Lines 1–3: We import the relevant packages and classes.
-
Lines 8–14: We define a function called
sleep()that makes the current thread sleep for the given amount of time. -
Lines 16–18: We define a function called
executionThread()that prints the current thread of execution. -
Lines 22–28: We create a completable future called
completableFuture1using thesupplyAsyc()method. We pass a supplier to it that sleeps for one second and then calls theexecutionThread(). This method returns a string value. -
Lines 30–36: We create another completable future called
completableFuture2using thesupplyAsyc()method. We pass a supplier to it that sleeps for two seconds and then calls theexecutionThread(). This method also returns a string value. -
Line 38: We create a list of completable futures called
completableFuturesfromcompletableFuture1andcompletableFuture2. -
Line 40: We pass the
completableFuturesto theanyOf()method. Here, a new future,resultantCf, is returned. -
Line 42: When any of the passed futures is completed,
resultantCfwill be in the completed stage. We print the value of theresultantCf. -
Line 45: The main thread sleeps for one second.