What is Java Executor Framework?
Last Updated :
19 Aug, 2025
With modern multi-core processors and demand for high-performance apps, multithreading APIs are widely used. Java provides the Executor Framework (java.util.concurrent.Executor, introduced in JDK 5) to run Runnable tasks without creating new threads every time, by reusing existing ones.
The Executors class offers factory methods to create thread pools.
- Threads stay alive and are reused.
- Extra tasks are placed in a queue and free threads pick them up later.
- By default, these queues are unbounded in JDK executors.
Types of Executors in Java
Below are the commonly used executor types:
- SingleThreadExecutor
- FixedThreadPool(n)
- CachedThreadPool
- ScheduledExecutor
Let us discuss these popular Java executors in some detail and what exactly they do to get a better idea before implementing the same.
1. SingleThreadExecutor
A thread pool of a single thread can be obtained by calling the static newSingleThreadExecutor() method of the Executors class. It is used to execute tasks sequentially.
Syntax:
ExecutorService executor = Executors.newSingleThreadExecutor();
2. FixedThreadPool
As the name indicates, it is a thread pool of a fixed number of threads. The tasks submitted to the executor are executed by the n threads and if there are more task, they are stored on a LinkedBlockingQueue. It uses Blocking Queue.
Syntax:
ExecutorService fixedPool = Executors.newFixedThreadPool(2);
3. CachedThreadPool
Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. It uses a SynchronousQueue queue.
Syntax:
ExecutorService executorService = Executors.newCachedThreadPool();
4. ScheduledExecutor
Scheduled executors are based on the interface ScheduledExecutorService which extends the ExecutorService interface. This executor is used when we have a task that needs to be run at regular intervals or if we wish to delay a certain task.
Syntax:
ScheduledExecutorService scheduledExecService = Executors.newScheduledThreadPool(1);
The tasks can be scheduled using either of the two methods:
- scheduleAtFixedRate: This schedules tasks to start at fixed intervals, but if a task takes longer than the interval, the next execution will be delayed. It does not interrupt the running task, but, upcoming executions are queued and wait for the current one to finish
- scheduleWithFixedDelay: This will start the delay countdown only after the current task completes.
Syntax:
scheduledExecService.scheduleAtFixedRate
(Runnable command, long initialDelay, long period, TimeUnit unit)
Future Object in Executor Framework
The result of the task submitted for execution to an executor can be accessed using the java.util.concurrent. The future object returned by the executor. Future can be thought of as a promise made to the caller by the executor. The future interface is mainly used to get the results of Callable results. whenever the task execution is completed, it is set in this future object by the executor.
Syntax:
Future<String> result = executorService.submit(callableTask);
Java Program to Create and Execute a Simple Executor
Implementation: Creating and Executing a Simple Executor in which we will create a task and execute it in a fixed pool
- The Task class implements Callable and is parameterized to String type. It is also declared to throw Exception.
- Now in order to execute task in class “Task” we have to instantiate the Task class and are passing it to the executor for execution.
- Print and display the result that is returned by the Future object
Example
Java
import java.util.concurrent.*;
class Task implements Callable<String> {
private String message;
public Task(String message)
{
this.message = message;
}
public String call() throws Exception
{
return "Hi " + message + "!";
}
}
public class Geeks {
public static void main(String[] args)
{
Task task = new Task("GeeksForGeeks");
// Creating object of ExecutorService class and Future object Class
ExecutorService executorService = Executors.newFixedThreadPool(4);
Future<String> result = executorService.submit(task);
// Try block to check for exceptions
try {
System.out.println(result.get());
}
// Catch block to handle the exception
catch (InterruptedException | ExecutionException e) {
System.out.println("Error occurred while executing the submitted task");
e.printStackTrace();
}
// Cleaning resource and shutting down JVM by saving JVM state using shutdown() method
executorService.shutdown();
}
}
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java