Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is CompletableFuture.runAfterBoth() in Java?

abhilash

Overview

runAfterBoth() is an instance method of the CompletableFuture class, which tracks the completion of the given completion stage/completable future and invokes a runnable method after it gets completed.

The runAfterBoth method is defined in the CompletableFuture class. The CompletableFuture class is defined in the java.util.concurrent package. To import the CompletableFuture class, we check the following import statement:

import java.util.concurrent.CompletableFuture;

Syntax


public CompletableFuture<Void> runAfterBoth(CompletionStage<?> other, Runnable action)

Parameters

  • CompletionStage<?> other: The completable future or the completion stage to execute.
  • Runnable action: The runnable to execute.

Return value

This method returns a new CompletableFuture.

Code example

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) {
        CompletableFuture<String> completableFutureObject1 = CompletableFuture.supplyAsync(() -> {
            sleep(1000);
            String stringToPrint = "Hello-Educative";
            System.out.println("----\nsupplyAsync second future - " + stringToPrint);
            executionThread();
            return stringToPrint;
        });

        CompletableFuture<String> completableFutureObject2 = CompletableFuture.supplyAsync(() -> {
            sleep(2000);
            String stringToPrint = "Hello-Edpresso";
            System.out.println("----\nsupplyAsync second future - " + stringToPrint);
            executionThread();
            return stringToPrint;
        });

        Runnable runnable = () -> System.out.println("Completed both the futures");

        completableFutureObject1.runAfterBoth(completableFutureObject2, runnable);
        sleep(3000);
    }
}
Implementation of the "runAfterBoth" method

Code explanation

  • Line 1: We import the relevant packages and classes.
  • Lines 5–11: We define a function called sleep() that makes the current thread sleep for the given amount of milliseconds.
  • Lines 13–15: We define a function called executionThread() that prints the current thread of execution.
  • Lines 18–24: We create a completable future object called completableFutureObject1, by using the supplyAsyc() method and passing a supplier that sleeps for 1 second, invokes the executionThread() method, and returns a string value.
  • Lines 26–32: We create another completable future object called completableFutureObject2, by using the supplyAsyc() method and passing a supplier that sleeps for 2 seconds, invokes the executionThread() method, and returns a string value.
  • Line 34: We create a runnable that prints a string to the console.
  • Line 36: We invoke the runAfterBoth method on completableFutureObject1 by passing completableFutureObject2 and runnable as method arguments. The passed runnable is executed only after completableFutureObject1 and completableFutureObject2 are completed.
  • Line 37: The main thread sleeps for 3 seconds.

RELATED TAGS

java
communitycreator
RELATED COURSES

View all Courses

Keep Exploring