Java ScheduledThreadPoolExecutor

In this tutorial, we will learn about java.util.concurrent.ScheduledThreadPoolExecutor, its features with explanation including the working examples of it.

ScheduledThreadPoolExecutor

In Java, java.util.concurrent.ScheduledThreadPoolExecutor is a class under the package of java.util.concurrent and it provides a thread pool for executing tasks at a specified time or after a specified delay. And it extends the ThreadPoolExecutor class and adds the capability to schedule tasks to run at a specific time or after a specific delay.

ScheduledThreadPoolExecutor is a useful class in the concurrent package for scheduling tasks to run at a specific time or after a specific delay. It provides a fixed thread pool for executing tasks and making it suitable for use in multi-threaded applications.

Features of ScheduledThreadPoolExecutor

  1. Task scheduling: It can schedule tasks to run at a specific time or after a specific delay.
  2. Fixed thread pool: It maintains a fixed number of threads in the thread pool to execute tasks.
  3. Thread safety: It is thread-safe, which means multiple threads can access it without any issues.
  4. Customizable thread factory: It allows for a custom thread factory to be used, which can provide additional configuration options for the threads in the thread pool.

ScheduledThreadPoolExecutor example one:

In this example, we will schedule a task to execute after two seconds.

CopiedCopy Code
import java.util.Date;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ScheduledThreadPoolExecutorExample {
    public static void main(String[] args) throws InterruptedException {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
        Runnable task = new Runnable() {
			@Override
			public void run() {
				System.out.println("Task executed time :"+new Date());
			}
		};
		System.out.println("Task submitted time :"+new Date());
        executor.schedule(task, 2, TimeUnit.SECONDS);
        executor.shutdown();
    }
}

Output:

Task submitted time :Fri Apr 28 21:47:20 IST 2023
Task executed time :Fri Apr 28 21:47:22 IST 2023

Explanation :

In the above example, we create a ScheduledThreadPoolExecutor with a fixed pool size of 1. We then create a Runnable task that prints when the task ran time. We schedule the task to run once after a delay of 2 seconds using the schedule() method, before scheduling task printed current time of the system for better understanding on the example. And finally, we shut down the executor using the shutdown() method otherwise example will never exit.

ScheduledThreadPoolExecutor example two:

In this example, we will schedule a task to run repeatedly at a fixed interval of 1 second for each task.

CopiedCopy Code
import java.util.Date;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ScheduledThreadPoolFixedRateExample {
    public static void main(String[] args) throws InterruptedException {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
        Runnable task = new Runnable() {
			@Override
			public void run() {
				System.out.println("Task executed at :"+ new Date());
			}
		};
        executor.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS);
        Thread.sleep(5000L);// to keep task to run 5seconds before shutting down
        executor.shutdown();
    }
}

Output :

Task executed at :Fri Apr 28 21:55:25 IST 2023
Task executed at :Fri Apr 28 21:55:26 IST 2023
Task executed at :Fri Apr 28 21:55:27 IST 2023
Task executed at :Fri Apr 28 21:55:28 IST 2023
Task executed at :Fri Apr 28 21:55:29 IST 2023

Explanation :

In the above example, we create a ScheduledThreadPoolExecutor with a fixed pool size of 1. Then we created a Runnable task that prints current time. We schedule the task to run repeatedly at a fixed interval of 1 second using the scheduleAtFixedRate() method. The 0 parameter specifies an initial delay of 0 seconds, meaning the task will start running immediately, before shutting down we made main thread to get sleep 5seconds, so that task can run 5times. Later we shut down the scheduled executor otherwise program will never exit.

Conclusion :

In this tutorial, we have covered what is ScheduledThreadPoolExecutor and its features along with the working examples of it.