The ScheduledThreadPoolExecutor
is a class that extends ThreadPoolExecutor
and provides functionality for scheduling tasks to run after a specified delay or at a fixed rate. It is useful for scheduling tasks that need to be run periodically or after a certain delay.
It provides the following methods:
schedule(Runnable command, long delay, TimeUnit unit)
: Schedules the specified command to run after a specified delay.schedule(Callable<V> callable, long delay, TimeUnit unit)
: Schedules the specified callable to run after a specified delay and returns aScheduledFuture<V>
that can be used to retrieve the result of the callable.scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
: Schedules a command to run periodically at a fixed rate, starting after an initial delay. The period is the amount of time between the end of one execution and the start of the next.scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
: Schedules a command to run periodically with a fixed delay between the end of one execution and the start of the next. The initial delay is the amount of time to wait before the first execution.submit(Runnable task)
: Submits a task for execution and returns aScheduledFuture
representing that task.submit(Callable<V> task)
: Submits a task for execution and returns aScheduledFuture
representing that task.submit(Runnable task, T result)
: Submits a task for execution and returns aScheduledFuture
representing that task.
These methods can be used to schedule tasks to run at a specific time, to run periodically at a fixed rate or with a fixed delay, or to submit tasks for execution and retrieve their results as ScheduledFuture
objects.
Here’s an example of using ScheduledThreadPoolExecutor
in Java:
import java.util.concurrent.*; public class ScheduledThreadPoolExample { public static void main(String[] args) { ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1); // Schedule a task to run after a delay of 5 seconds executorService.schedule(new Runnable() { @Override public void run() { System.out.println("Task 1: Delayed task running after 5 seconds"); } }, 5, TimeUnit.SECONDS); // Schedule a task to run repeatedly every 1 second with an initial delay of 2 seconds executorService.scheduleAtFixedRate(new Runnable() { @Override public void run() { System.out.println("Task 2: Running every 1 second"); } }, 2, 1, TimeUnit.SECONDS); // Shutdown the executor service after all scheduled tasks have been completed executorService.shutdown(); } }
In this example, we create a ScheduledExecutorService
with a thread pool size of 1 using the Executors.newScheduledThreadPool
method. We then schedule two tasks to run using the schedule
and scheduleAtFixedRate
methods.
The first task is scheduled to run after a delay of 5 seconds using the schedule
method. The second task is scheduled to run every 1 second with an initial delay of 2 seconds using the scheduleAtFixedRate
method.
Finally, we shutdown the executor service using the shutdown
method. This ensures that all scheduled tasks have been completed before the program exits.
Overall, the ScheduledThreadPoolExecutor
is a useful class for scheduling tasks to run periodically or after a certain delay in a controlled manner.