![]() |
VOOZH | about |
ScheduledExecutorService is used to schedule tasks to run after a delay or at fixed intervals in Java. It is part of the java.util.concurrent package and extends ExecutorService. It helps in managing background and periodic task execution efficiently.
public interface ScheduledExecutorService extends ExecutorService
Output:
A count-down-clock program that counts from 10 to 0
Current time : 28
Number 10 Current time : 28
Number 9 Current time : 29
Number 8 Current time : 30
Number 7 Current time : 31
Number 6 Current time : 32
Number 5 Current time : 33
Number 4 Current time : 34
Number 3 Current time : 35
Number 2 Current time : 36
Number 1 Current time : 37
Number 0 Current time : 38
Explanation: This example demonstrates a countdown clock from 10 to 0 using ScheduledExecutorService. A scheduler is created using Executors.newScheduledThreadPool(), and tasks are scheduled with delays of (10 - i) seconds.
Note: Execution time may vary depending on the system clock.
It extends ExecutorService, which extends Executor, enabling scheduling of tasks with delays or at fixed intervals.
Implementing class: The implementing class of ScheduledExecutorService is ScheduledThreadPoolExecutor.
newScheduledThreadPool(int corePoolSize)Syntax:
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
Syntax:
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory)
METHOD | DESCRIPTION |
|---|---|
| schedule(Runnable command, long delay, TimeUnit unit) | Submits a one-shot task that becomes enabled after the given delay. |
| scheduleâ(Callable<V> callable, long delay, TimeUnit unit) | Submits a value-returning one-shot task that becomes enabled after the given delay. |
| scheduleAtFixedRateâ(Runnable command, long initialDelay, long period, TimeUnit unit) | Submits a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is, executions will commence after initialDelay, then initialDelay + period, then initialDelay + 2 * period, and so on. |
| scheduleWithFixedDelayâ(Runnable command, long initialDelay, long delay, TimeUnit unit) | Submits a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next. |
METHOD | DESCRIPTION |
|---|---|
| executeâ(Runnable command) | Executes the given command at some time in the future. |
METHOD | DESCRIPTION |
|---|---|
| awaitTerminationâ(long timeout, TimeUnit unit) | Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first. |
| invokeAllâ(Collection<? extends Callable<T>> tasks) | Executes the given tasks, returning a list of Futures holding their status and results when all complete. |
| invokeAllâ(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) | Executes the given tasks, returning a list of Futures holding their status and results when all complete or the timeout expires, whichever happens first. |
| invokeAnyâ(Collection<? extends Callable<T>> tasks) | Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do. |
| invokeAnyâ(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) | Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do before the given timeout elapses. |
| isShutdown() | Returns true if this executor has been shut down. |
| isTerminated() | Returns true if all tasks have completed following shut down. |
| shutdown() | Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. |
| shutdownNow() | Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution. |
| submitâ(Runnable task) | Submits a Runnable task for execution and returns a Future representing that task. |
| submitâ(Runnable task, T result) | Submits a Runnable task for execution and returns a Future representing that task. |
| submitâ(Callable<T> task) | Submits a value-returning task for execution and returns a Future representing the pending results of the task. |