When to use the delayedExecutor() method of CompletableFuture in Java 9?



In this article, we will learn to use the delayedExecutor() method of CompletableFuture in Java 9. We're gonna know about the delayedExecutor() method and also the CompletableFuture class, as this method belongs to this class, and we're gonna know the uses of the delayedExecutor() method along with an example.

CompletableFuture Class

The CompletableFuture Class was introduced in Java 8. CompletableFuture is a class in java.util.concurrent package that implements the Future and CompletionStage Interface.

CompletableFuture provides a powerful and flexible way to write asynchronous, non-blocking code. It supports delays and timeouts.

The delayedExecutor() Method

The delayedExecutor() method has been added to the CompletableFuture class in Java 9. CompletableFuture defines two overloaded methods of delayedExecutor().

The first method returns an Executor object from the default Executor object that the CompletableFuture object uses to execute the task after the delay, and a new Executor object can be used for task execution.

The second method also returns an Executor object, but it is an Executor object that we pass into this method after the delay, and a new Executor object can also do task execution.

Syntax

Below is the syntax of the delayedExecutor() method declaration:

public static Executor delayedExecutor(long delay, TimeUnit unit, Executor executor)
public static Executor delayedExecutor(long delay, TimeUnit unit)

Parameters:

  • delay: It represents how long to delay, in units of time.
  • unit: This represents a TimeUnit(e.g., TimeUnit.MILLISECONDS) determining how to interpret the delay parameter.
  • executor: This is the base executor.

Uses of the delayedExecutor() Method

The following are some of the use cases of the delayedExecutor() method in Java:

  • Delayed Asynchronous Execution: It allows you to run tasks after a certain delay, useful for timeouts or scheduled actions.
  • Timeout Handling: The delayedExecutor() method can be used to mimic timeouts or fallback behaviors in asynchronous pipelines.
  • Non-Blocking Delay: Unlike Thread.sleep(), it doesn't block the main thread. Rather, it schedules the task to run later without crashing the application.

Example of delayedExecutor() Method

Below is an example of the delayedExecutor() method in Java:

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class DelayedExecutorMethodTest {
   public static void main(String args[]) throws InterruptedException, ExecutionException {
      CompletableFuture future = new CompletableFuture<>();
      future.completeAsync(() -> {
         try {
            System.out.println("inside future: processing data...");
            return "tutorialspoint.com";
         } catch(Throwable e) {
            return "not detected";
         }
      }, CompletableFuture.delayedExecutor(3, TimeUnit.SECONDS))
                          .thenAccept(result -> System.out.println("accept: " + result));
      for(int i = 1; i <= 5; i++) {
         try {
            Thread.sleep(1000);
         } catch(InterruptedException e) {
            e.printStackTrace();
         }
         System.out.println("running outside... " + i + " s");
      }
   }
}

Output

running outside... 1 s
running outside... 2 s
inside future: processing data...
accept: tutorialspoint.com
running outside... 3 s
running outside... 4 s
running outside... 5 s
Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-06-16T16:32:44+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements