Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
allof
completablefuture

What is CompletableFuture.allOf() in Java?

abhilash

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);
    }
}
Implementation of the allOf() method

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 completableFuture1 using the supplyAsyc() method by passing a supplier that sleeps for 1 second, invokes the executionThread() method, and returns a string value.
  • Lines 30 to 36: We create another completable future called completableFuture2 using the supplyAsyc() method by passing a supplier that sleeps for 2 seconds, invokes the executionThread() method, and returns a string value.
  • Line 38: We create a list of completable future called completableFutures from completableFuture1 and completableFuture2.
  • Line 40: We pass the completableFutures to the allOf() method. Here, a new future is returned called resultantCf.
  • Line 42: Once all the passed futures are completed, 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.
  • Line 44: We print the results of the individual futures.
  • Line 45: The main thread sleeps for 1 second.

RELATED TAGS

java
communitycreator
allof
completablefuture
RELATED COURSES

View all Courses

Keep Exploring