In Java, there are certain situations that require intervals to be placed in accordance with the requirements. For instance, executing the functionalities periodically from time to time based on the set interval and the number of times to execute. In such situations, the “ScheduledExecutorService” interface in Java comes into effect that performs these types of functionalities in a streamlined manner.
This article will elaborate on the Java “ScheduledExecutorService” interface.
What is ScheduledExecutorService Interface in Java?
The “ScheduledExecutorService” interface in Java is a subinterface of the “ExecutorService” interface included in the “java.util.concurrent” package. This interface is utilized to execute the given tasks periodically or once after a specified delay.
As “ScheduledExecutorService” is an interface, therefore it can’t be instantiated. But the “Executors” class, in the “java.util.concurrent” package, provides the “newScheduledThreadPool()” method that returns the “ScheduledExecutorService” object corresponding to its implementing classes.
“ScheduledExecutorService” Methods
Method | Functionality |
---|---|
schedule(Callable<V> callable, long delay, TimeUnit unit) | It runs a ScheduledFuture that is enabled after the provided delay. |
schedule(Runnable command, long time, TimeUnit unit) | It runs the desired action that is enabled after the specified time/delay. |
scheduleAtFixedRate(Runnable command, long startDelay, long period, TimeUnit unit) | This method runs a periodic action that is enabled firstly after the specified startDelay, and subsequently with the given time i.e., period. It is such that the executions will initiate after initialDelay then initialDelay+time, then initialDelay + 2 * time, and so on. |
scheduleWithFixedDelay(Runnable command, long startDelay, long delay, TimeUnit unit) | It submits a periodic action that is enabled firstly after the provided startDelay, and subsequently with the specified delay between the cancellation of one execution and the start of the next. |
Import the below-given package in the example to enable access to all the functionalities in the “java.util.concurrent” package:
import java.util.concurrent.*;
Example 1: Utilization of the “ScheduledExecutorService” Interface and its Methods in Java
This example explains the utilization of the discussed interface in thread-based environment:
import java.util.concurrent.*;public class ExecutorService {
public static void main(final String[] arguments) throws InterruptedException {
final ScheduledExecutorService x = Executors.newScheduledThreadPool(2);
final ScheduledFuture<?> y = x.scheduleAtFixedRate(new Beeping(), 2, 2, TimeUnit.SECONDS);
x.schedule(new Runnable() {
public void run() {
y.cancel(true);
x.shutdown();
}}, 5, TimeUnit.SECONDS);
}
static class Beeping implements Runnable {
public void run() {
System.out.println("Tic Toc");
}
}}
According to the above block of code:
- First, import the below-given package in the example to enable access to all the functionalities in the “java.util.concurrent” package:
- Create a “ScheduledExecutorService” object having the stated pool size that signifies the number of alive threads.
- After that, apply the “scheduleAtFixedRate()” method to execute a periodic action that enables first after the specified initial delay, and subsequently with the provided period. It is such that the executions will start after initialDelay, then initialDelay+period, and then initialDelay + 2 * period, and so on.
- Also, apply the “schedule()” method that specifies the number of times the message “Tic Toc” executes after the specific delay.
Output

As seen, the stated message is displayed after the set time and also the specified number of times.
Example 2: Applying the “scheduleWithFixedDelay()” Method
This demonstration illustrates the implementation of the “scheduleWithFixedDelay()” method:
import java.util.concurrent.*;
import java.util.*;
public class ScheduledExecutor {
public static void main(String[] args) {
ScheduledExecutorService x = Executors.newScheduledThreadPool(1);
ScheduledFuture y = x.scheduleWithFixedDelay(new displayTime(), 3, 2, TimeUnit.SECONDS);
x.schedule(new Runnable() {
public void run() {
y.cancel(true);
x.shutdown();
System.out.println("Logging off...");
}}, 10, TimeUnit.SECONDS);
}}
class displayTime implements Runnable {
public void run() {
System.out.println(new Date());
}}
In this code, import the additional “java.util.*” package to access all the features in the “java.util” package. After that, the same demonstration is carried out. The difference is that the “scheduleWithFixedDelay()” method executes until it invokes the “cancel()” method that cancels the task. Also, in this code, the Date is displayed with the intervals instead via the “new” keyword and the “Date()” constructor.
Output

Conclusion
The Java “ScheduledExecutorService” interface corresponds to a subinterface of “ExecutorService” to execute the provided tasks periodically or once after a specific delay. This guide was all about the Java “ScheduledExecutorService” interface.