Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
thenacceptasync
completablefuture

What is CompletableFuture.thenAcceptAsync() in Java?

abhilash

thenAcceptAsync() is an instance method in Java. It is used when we don’t want to return anything from our callback function and only want to run some code once a FutureThis represents the result of an asynchronous computation. completes.

The thenAcceptAsync() method has access to the result of the CompletableFuture on which it is attached. The thenAcceptAsync() method runs the code in a thread that is obtained from the default executor/the common pool of ForkJoinPool. The method optionally accepts an Executor. When a custom executor is specified, the code is executed in a thread that is obtained from the passed executor.

The thenAcceptAsync 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> thenAcceptAsync(Consumer<? super T> action)

Parameters

  • Consumer<? super T> action: This is the code that needs to be executed.

Return value

This method returns a CompletableFuture.

Code

import java.util.concurrent.*;
import java.util.function.Consumer;

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> completableFuture = CompletableFuture.supplyAsync(() -> {
            sleep(1000);
            executionThread();
            return "Educative";
        });
        
        Consumer<String> consumer = res -> {
            executionThread();
            System.out.println("Future result - " + res);
        };

        completableFuture.thenAcceptAsync(consumer);
        sleep(2000);
    }
}
Implementation of the "thenAcceptAsync" method

Code explanation

  • Lines 1–2: We import the relevant packages and classes.

  • Lines 6–12: We define a function called sleep(), which makes the current thread sleep for the given amount of time (in milliseconds).

  • Lines 14–16: We define a function called executionThread(), which prints the current thread of execution.

  • Lines 20–24: We create a completable future called completableFuture, using the supplyAsyc() method. We pass a supplier to it, which sleeps for one second and then invokes the executionThread(). Finally, this method returns a string value.

  • Lines 26–29: We define a consumer, consumer, which consumes the result of the future that is executed once the completableFuture is complete. The consumer invokes the executionThread() method and prints the future result.

  • Line 33: We invoke the thenAcceptAsync method on completableFuture. We pass consumer as an argument to the method.

  • Line 35: The main thread is made to sleep for two seconds.

RELATED TAGS

java
communitycreator
thenacceptasync
completablefuture
RELATED COURSES

View all Courses

Keep Exploring