Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the CompletableFuture.anyOf() method in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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);
}
}
Implementation of the "anyOf" method

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 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.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring