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;
public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs)
CompletableFuture<?>... cfs
: The list of completable futures.
The anyOf()
method returns a new CompletableFuture
.
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);}}
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 completableFuture1
using the supplyAsyc()
method. We pass a supplier to it that sleeps for one second and then calls the executionThread()
. This method returns a string value.
Lines 30–36: We create another completable future called completableFuture2
using the supplyAsyc()
method. We pass a supplier to it that sleeps for two seconds and then calls the executionThread()
. This method also returns a string value.
Line 38: We create a list of completable futures called completableFutures
from completableFuture1
and completableFuture2
.
Line 40: We pass the completableFutures
to the anyOf()
method. Here, a new future, resultantCf
, is returned.
Line 42: When any of the passed futures is completed, resultantCf
will be in the completed stage. We print the value of the resultantCf
.
Line 45: The main thread sleeps for one second.