ScheduledExecutorService

An {@link ExecutorService} that can schedule commands to run after a given delay, or to execute periodically.

<p>The {@code schedule} methods create tasks with various delays and return a task object that can be used to cancel or check execution. The {@code scheduleAtFixedRate} and {@code scheduleWithFixedDelay} methods create and execute tasks that run periodically until cancelled.

<p>Commands submitted using the {@link Executor#execute(Runnable)} and {@link ExecutorService} {@code submit} methods are scheduled with a requested delay of zero. Zero and negative delays (but not periods) are also allowed in {@code schedule} methods, and are treated as requests for immediate execution.

<p>All {@code schedule} methods accept <em>relative</em> delays and periods as arguments, not absolute times or dates. It is a simple matter to transform an absolute time represented as a {@link java.util.Date} to the required form. For example, to schedule at a certain future {@code date}, you can use: {@code schedule(task, date.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS)}. Beware however that expiration of a relative delay need not coincide with the current {@code Date} at which the task is enabled due to network time synchronization protocols, clock drift, or other factors.

<p>The {@link Executors} class provides convenient factory methods for the ScheduledExecutorService implementations provided in this package.

<h3>Usage Example</h3>

Here is a class with a method that sets up a ScheduledExecutorService to beep every ten seconds for an hour:

<pre> {@code import static hunt.concurrency.TimeUnit.*; class BeeperControl { private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

public void beepForAnHour() { Runnable beeper = () -> System.out.println("beep"); ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS); Runnable canceller = () -> beeperHandle.cancel(false); scheduler.schedule(canceller, 1, HOURS); } }}</pre>

@author Doug Lea

Members

Functions

schedule
ScheduledFuture!void schedule(Runnable command, Duration delay)

Submits a one-shot task that becomes enabled after the given delay.

scheduleAtFixedRate
ScheduledFuture!void scheduleAtFixedRate(Runnable command, Duration initialDelay, Duration period)

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 {@code initialDelay}, then {@code initialDelay + period}, then {@code initialDelay + 2 * period}, and so on.

scheduleWithFixedDelay
ScheduledFuture!void scheduleWithFixedDelay(Runnable command, Duration initialDelay, Duration delay)

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.

Inherited Members

From ExecutorService

shutdown
void shutdown()

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.

shutdownNow
List!(Runnable) 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.

isShutdown
bool isShutdown()

Returns {@code true} if this executor has been shut down.

isTerminated
bool isTerminated()

Returns {@code true} if all tasks have completed following shut down. Note that {@code isTerminated} is never {@code true} unless either {@code shutdown} or {@code shutdownNow} was called first.

awaitTermination
bool awaitTermination(Duration timeout)

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.

Meta