Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
getnow
completablefuture

What is CompletableFuture.getNow() in Java?

abhilash

Overview

The getNow method is an instance method which is defined in the CompletableFuture class. The CompletableFuture class is defined in the java.util.concurrent package. This method returns the result of the CompletableFuture, if it’s available. Otherwise, it returns the passed value.

We can import the CompletableFuture class by using the following statement:

import java.util.concurrent.CompletableFuture;

Syntax


public T getNow(T valueIfAbsent)

Parameters

  • T valueIfAbsent: This is the value that needs to be returned if the result of the future is not yet available.

Return value

This method returns the result of the future, if completed. Otherwise, it returns the passed value.

Code

import java.util.concurrent.*;


public class Main {

    private static String process(){
        sleep(2000);
        System.out.println("Current Execution thread where the supplier is executed - " + Thread.currentThread().getName());
        return "Hello Educative";
    }

    private static CompletableFuture<String> createFuture(){
        return CompletableFuture.supplyAsync(Main::process);
    }

    private static void sleep(int millis){
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        CompletableFuture<String> stringCompletableFuture = createFuture();

        String valueToReturn = "Result not yet available";

        String value = stringCompletableFuture.getNow(valueToReturn);

        sleep(1000);

        System.out.println("Completed Processing. Returned value - " + value);

    }
}
Implementation of the getNow() method

Code explanation

  • Line 1: We import the relevant packages and classes.
  • Lines 6–10: We define a function called process, which prints the thread that executes the function, sleeps for one second, and returns a string.
  • Lines 12–14: We define a function called createFuture, which uses the supplyAsync() method to run the process() method in the common pool of ForkJoinPool. This function returns a CompletableFuture.
  • Lines 16–22: We define a function called sleep(), which makes the current thread sleep for the given amount of milliseconds.
  • Line 25: We get the CompletableFuture by invoking the createFuture method.
  • Line 27: We define the value as valueToReturn to return in case the future’s result is not available.
  • Line 29: We retrieve the value of the future, using the getNow() method. The valueToReturn is passed as an argument to the method.
  • Line 31: We invoke the sleep function on the main thread.
  • Line 33: We print the value that is returned by the getNow() method.

RELATED TAGS

java
communitycreator
getnow
completablefuture
RELATED COURSES

View all Courses

Keep Exploring