Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
defaultexecutor
completablefuture

What is CompletableFuture.defaultExecutor() in Java?

abhilash

Overview

defaultExecutor() is an instance method of the CompletableFuture class. This method is used to get the default pool of threads that are used by the async methods of the CompletableFuture class when a custom executor is not specified.

The defaultExecutor 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 Executor defaultExecutor()

Parameters

This method has no parameters.

Return value

This method returns an instance of Executor.

Code

import java.util.concurrent.*;

public class Main {

    public static void main(String[] args){
        CompletableFuture<String> completableFuture = CompletableFuture.completedFuture("hello-educative");
        System.out.println(completableFuture.defaultExecutor());
    }
}
Implementation of the "defaultExecutor" method

Code explanation

  • Line 1: We import the relevant packages.
  • Line 6: We define a CompletableFuture, using the completedFuture static method of the CompletableFuture class.
  • Line 7: We print the default executor. ForkJoinPool is the default executor for the async methods.

Note: The ForkJoinPool is a specialized implementation of ExecutorService implementing the work-stealing algorithm.

RELATED TAGS

java
communitycreator
defaultexecutor
completablefuture
RELATED COURSES

View all Courses

Keep Exploring