ExecutorService, ScheduledExecutorService
The java.util.concurrent package provides the Executor Framework as an additional tool or package library for Java developers to simplify the concept of multithreaded programming tasks in the Java programming language.

Instead of manually creating a custom, user-based new Thread task for each new Thread task design, Java developers can use Executor Services to manage or control a pool of existing threads and execute Thread tasks.
There are two essential Executor Framework interfaces in the Java programming language.
- ExecutorService – It is used to execute and manage asynchronous Thread tasks in a Java program.
- ScheduledExecutorService – It is used to delay or repeatedly execute an existing Java Thread task at a fixed time.
ExecutorService in Java Thread Concept.
ExecutorService is an interface from the java.util.concurrent package in Java programs that provides a simple method for managing and executing multiple thread tasks using a pool of user-created thread tasks.
Instead of writing it like this.
SimpleThread tsk1 = new SimpleThread(task1);
SimpleThread tsk2 = new SimpleThread(task2);
SimpleThread tsk3 = new Thread(task3);
tsk1.start();
tsk2.start();
tsk3.start();
This allows Java users to create a thread task pool:
ExecutorService executor = Executors.newFixedThreadPool(3);
and submit tasks to it as needed.
Where the thread executor focuses on assigning tasks to available threads.
A basic example of a Java thread ExecutorService.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
public static void main(String[] args) {
ExecutorService executor =
Executors.newFixedThreadPool(3);
executor.execute() -> {
System.out.println(“Thread Task 1 is still continuing”);
});
executor.execute() -> {
System.out.println(“Thread Task 2 is still continuing”);
});
executor.execute() -> {
System.out.println(“Thread Task 3 is still continuing”);
});
executor.shutdown();
}
}
Explanation of Java thread ExecutorService.
Executors.newFixedThreadPool(3)
This creates a pool of three worker threads for the current thread task.
Then.
executor.execute(task);
Submits a task for execution.
When all the necessary thread tasks have been submitted in the proper order in this process, we must then properly shut down the executor.
executor.shutdown();
This indicates to the service executor that no new thread tasks will be accepted, while previously submitted thread tasks may be terminated during this process.
Why use ExecutorService in Java threads?
There are several advantages to using the ExecutorService package library in Java thread tasks.
Reusing multiple threads.
Instead of creating a new thread for each new task, existing worker threads can execute multiple tasks one by one.
Thread Task 1 ──┐
Thread Task 2 ──┼──> Thread Pool
Thread Task 3 ──┘
↓
Worker Threads
Better Resource Management.
Creating too many threads and tasks in a single program can use up a lot of your existing system resources. Meanwhile, the thread pool helps us control the number of worker threads running simultaneously.
Better Thread Task Management.
The Thread Task Executor provides you with methods for this.
- Submitting a thread task
- Providing an output result
- Closing the thread task execution process
- Managing multiple thread tasks simultaneously
execute() method concept in Java threads.
The execute() function method in a Java thread task is used when the Java user does not require a return value.
executor.execute(() -> {
System.out.println(“Welcome to Vcanhelpsu”);
});
This function submits a user-generated thread task for asynchronous execution.
submit() method concept in Java threads.
The submit() function method in a Java thread task can be used when the Java user wants to achieve a result or track the completion of a thread task.
It returns a future information value.
Example of the submit() method concept.
import java.util.concurrent.*;
public class Main {
public static void main(String[] args) {
ExecutorService executor =
Executors.newFixedThreadPool(2);
Future<Integer> output = executor.submit(() -> {
return 7 * 5;
});
try {
System.out.println(output.get());
} catch (InterruptedException | ExecutionException ex) {
ex.printStackTrace();
}
executor.shutdown();
}
}
Program output.
35
In this.
Future<Integer> output
This displays the result you get after the thread’s task is completed.
Common types of ExecutorService in Java threads.
The Executors class provides Java users with a factory method for developing multiple thread designs.
Fixed Java thread pool.
ExecutorService executor =
Executors.newFixedThreadPool(3);
This creates a pool with a fixed number of worker threads in the current program.
Single Java thread Executor.
ExecutorService executor =
Executors.newSingleThreadExecutor();
This uses a single worker thread to execute thread tasks in Java one after another.
Cached thread pool concept.
ExecutorService executor =
Executors.newCachedThreadPool();
This dynamically creates and/or reuses threads based on the workload submitted to the current program.
For modern Java applications, the Java user should select the executor based on workload and lifecycle requirements, rather than simply using the default factory method.
ScheduledExecutorService concept in Java threads.
ScheduledExecutorService is a specific executor method in Java threads. It is used when you need to complete a thread task:
- After a fixed time
- Or periodically as needed
- At a fixed rate
- With a fixed time delay between thread task executions
It extends the ExecutorService in Java thread tasks.
java threadExecutorService
↑
|
ScheduledExecutorService
This is a package library available in:
java.util.concurrent
Scheduling a Java thread task after a delay.
Java users can use this in their thread task method.
schedule()
Example of ScheduledExecutorService in a Java thread.
import java.util.concurrent.*;
public class Main {
public static void main(String[] args) {
ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
scheduler.schedule(() -> {
System.out.println(“Here this Task runs after 4 seconds”);
}, 4, TimeUnit.SECONDS);
scheduler.shutdown();
}
}
In this program, the thread task runs approximately 4 seconds after it is scheduled.
scheduleAtFixedRate() function in Java threads.
In a Java thread task program, this method repeatedly completes a task at a fixed rate.
Example of the scheduleAtFixedRate() function.
import java.util.concurrent.*;
public class Main {
public static void main(String[] args) {
ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(() -> {
System.out.println(“User-Created Thread Task executed”);
}, 0, 3, TimeUnit.SECONDS);
}
}
The arguments in this example are.
Initial user-defined 3-second delay
↓
0
↓
Period
↓
Output after 3 seconds
In this program, the thread tasks initially have a 0-second delay, and then this task information is scheduled every 3 seconds according to fixed-rate scheduling rules.
scheduleWithFixedDelay() function in Java threads.
This method waits for a task in a Java thread to terminate, then waits for the previously indicated delay before restarting it.
scheduler.scheduleWithFixedDelay(
() -> {
System.out.println(“User-defined Thread Task executed”);
},
0,
3,
TimeUnit.SECONDS
);
The delay difference is crucial here.
Java thread fixed rate difference.
Start → 3 seconds → Start → 3 seconds → Start
The purpose of the Java user method here is to maintain the schedule of a regular task after a user-defined fixed delay.
Start → Finish → 3 seconds → Start → Finish → 3 seconds → Start
The delay for each thread task is calculated after the previous execution has finished.
Detail Explanation of ExecutorService vs ScheduledExecutorService in java thread
| # | Executorservice java thread task package | Scheduledexecutorservice java thread task package |
| 1. | Executorservice used to executes all user submitted thread tasks | Scheduledexecutorservice used to executes scheduled thread tasks |
| 2. | Executorservice package used for normal asynchronous thread task execution | Scheduledexecutorservice package used for delayed/periodic thread task execution |
| 3. | Executorservice package full supports execute() and submit() function method | Scheduledexecutorservice package also provides java use a scheduling function method |
| 4. | Executorservice does not provide you a scheduling method for itself | Scheduledexecutorservice supports schedule() function thread task method |
| 5. | Executorservice package suitable for general thread task execution process | Scheduledexecutorservice method suitable for timers and recurring thread tasks method |
| 6. | Executorservice example: it processes 70 independent jobs given by system | Scheduledexecutorservice example: run a thread task every 7 minutes |
Essential steps in a Java Executor Framework thread.
ExecutorService methods.
execute() function method.
submit() function method.
shutdown() function method.
shutdownNow() function method.
isShutdown() function method.
isTerminated() function method.
ScheduledExecutorService.
In addition to the Executor thread task methods, you also get several other essential functions:
schedule() function method.
scheduleAtFixedRate() function method.
scheduleWithFixedDelay() function method.
shutdown() vs. shutdownNow() function methods in a Java thread.
shutdown() function method.
executor.shutdown(); function method.
It stops a new task from being submitted to a Java thread, but it allows already submitted thread tasks to complete.
shutdownNow().
executor.shutdownNow(); function method
It attempts to stop the currently running thread task in the current program and returns the thread tasks that were waiting for the thread execution process. Thread task interruptions work in conjunction, so a running thread task cannot be closed immediately.
A simple example of a Java thread task in real life.
So, let’s imagine a company organization.
ExecutorService Java Thread Method Concept Explained.
For example, in a company organization, 7 employees are performing different company tasks.
Thread Task 1 ──→ Allotted to Employee 1
Thread Task 2 ──→ Allotted to Employee 2
Thread Task 3 ──→ Allotted to Employee 3
Thread Task 4 ──→ Allotted to Employee 4
Thread Task 5 ──→ Allotted to Employee 5
Thread Task 6 ──→ Allotted to Employee 6
Thread Task 7 ──→ Allotted to Employee 7
In this process, when a company employee terminates or finishes a task, that employee can accept another task.
ScheduledExecutorService concept.
Now, consider a Java user with a task that must complete at a specific time.
Submit Task Report
↓
Wait 7 minutes
↓
Submit Task Report
↓
Wait 7 minutes
↓
Submit Task Report
In this example, a ScheduledExecutorService is designed to handle a similar number of thread tasks.
Conclusion of ExecutorService, ScheduledExecutorService Java Thread.
To simplify programming Java thread tasks, ExecutorService provides an interface in Java’s java.util.concurrent package. It provides a framework for managing and executing multiple thread tasks using a pool of thread tasks. This concept reduces the need for Java users to manually create and manage individual thread tasks. Thread tasks can be submitted using methods like execute() and submit(), and the executor can be shut down using the shut down() method.
ScheduledExecutorService for Java threads is a popular extension of ExecutorService, which is used to execute a thread task after a fixed time delay or repeatedly at a fixed time. It provides Java users with functions or methods like schedule(), scheduleAtFixedRate(), and scheduleWithFixedDelay().
Easy way to remember ExecutorService in Java thread tasks.
- ExecutorService → It runs the “Run my task” function in the Java thread package library.
- ScheduledExecutorService → It runs the “Run my task later or repeatedly” function in the Java thread package library.

