# ScheduledExecutorService

public interface **ScheduledExecutorService** implements [ExecutorService](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutorService.html)

|---|---|---|
| Known Indirect Subclasses [ScheduledThreadPoolExecutor](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ScheduledThreadPoolExecutor.html) |---|---| | [ScheduledThreadPoolExecutor](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ScheduledThreadPoolExecutor.html) | A `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ThreadPoolExecutor.html` that can additionally schedule commands to run after a given delay, or to execute periodically. | |||

An `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutorService.html` that can schedule commands to run after a given
delay, or to execute periodically.

The `schedule` methods create tasks with various delays
and return a task object that can be used to cancel or check
execution. The `scheduleAtFixedRate` and
`scheduleWithFixedDelay` methods create and execute tasks
that run periodically until cancelled.

Commands submitted using the `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Executor.html#execute(java.lang.Runnable)`
and `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutorService.html` `submit` methods are scheduled
with a requested delay of zero. Zero and negative delays (but not
periods) are also allowed in `schedule` methods, and are
treated as requests for immediate execution.

All `schedule` methods accept *relative* delays and
periods as arguments, not absolute times or dates. It is a simple
matter to transform an absolute time represented as a `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Date.html` to the required form. For example, to schedule at
a certain future `date`, you can use: `schedule(task,
date.getTime() - System.currentTimeMillis(),
TimeUnit.MILLISECONDS)`. Beware however that expiration of a
relative delay need not coincide with the current `Date` at
which the task is enabled due to network time synchronization
protocols, clock drift, or other factors.

The `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Executors.html` class provides convenient factory methods for
the ScheduledExecutorService implementations provided in this package.

### Usage Example

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

     import static java.util.concurrent.TimeUnit.*;
     class BeeperControl {
       private final ScheduledExecutorService scheduler =
         Executors.newScheduledThreadPool(1);

       public void beepForAnHour() {
         final Runnable beeper = new Runnable() {
           public void run() { System.out.println("beep"); }
         };
         final ScheduledFuture<?> beeperHandle =
           scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
         scheduler.schedule(new Runnable() {
           public void run() { beeperHandle.cancel(true); }
         }, 60 * 60, SECONDS);
       }
     }

<br />

### Public Method Summary

|---|---|
| abstract \<V\> [ScheduledFuture](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ScheduledFuture.html)\<V\> | [schedule](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ScheduledExecutorService.html#schedule(java.util.concurrent.Callable<V>,%20long,%20java.util.concurrent.TimeUnit))([Callable](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Callable.html)\<V\> callable, long delay, [TimeUnit](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/TimeUnit.html) unit) Creates and executes a ScheduledFuture that becomes enabled after the given delay. |
| abstract [ScheduledFuture](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ScheduledFuture.html)\<?\> | [schedule](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ScheduledExecutorService.html#schedule(java.lang.Runnable,%20long,%20java.util.concurrent.TimeUnit))([Runnable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Runnable.html) command, long delay, [TimeUnit](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/TimeUnit.html) unit) Creates and executes a one-shot action that becomes enabled after the given delay. |
| abstract [ScheduledFuture](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ScheduledFuture.html)\<?\> | [scheduleAtFixedRate](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ScheduledExecutorService.html#scheduleAtFixedRate(java.lang.Runnable,%20long,%20long,%20java.util.concurrent.TimeUnit))([Runnable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Runnable.html) command, long initialDelay, long period, [TimeUnit](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/TimeUnit.html) unit) Creates and executes 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. |
| abstract [ScheduledFuture](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ScheduledFuture.html)\<?\> | [scheduleWithFixedDelay](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ScheduledExecutorService.html#scheduleWithFixedDelay(java.lang.Runnable,%20long,%20long,%20java.util.concurrent.TimeUnit))([Runnable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Runnable.html) command, long initialDelay, long delay, [TimeUnit](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/TimeUnit.html) unit) Creates and executes 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 Method Summary

From interface [java.util.concurrent.ExecutorService](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutorService.html)

|---|---|
| abstract boolean | [awaitTermination](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutorService.html#awaitTermination(long,%20java.util.concurrent.TimeUnit))(long timeout, [TimeUnit](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/TimeUnit.html) 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. |
| abstract \<T\> [List](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/List.html)\<[Future](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html)\<T\>\> | [invokeAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutorService.html#invokeAll(java.util.Collection<?%20extends%20java.util.concurrent.Callable<T>>))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<? extends [Callable](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Callable.html)\<T\>\> tasks) Executes the given tasks, returning a list of Futures holding their status and results when all complete. |
| abstract \<T\> [List](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/List.html)\<[Future](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html)\<T\>\> | [invokeAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutorService.html#invokeAll(java.util.Collection<?%20extends%20java.util.concurrent.Callable<T>>,%20long,%20java.util.concurrent.TimeUnit))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<? extends [Callable](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Callable.html)\<T\>\> tasks, long timeout, [TimeUnit](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/TimeUnit.html) 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. |
| abstract \<T\> T | [invokeAny](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutorService.html#invokeAny(java.util.Collection<?%20extends%20java.util.concurrent.Callable<T>>))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<? extends [Callable](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Callable.html)\<T\>\> tasks) Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do. |
| abstract \<T\> T | [invokeAny](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutorService.html#invokeAny(java.util.Collection<?%20extends%20java.util.concurrent.Callable<T>>,%20long,%20java.util.concurrent.TimeUnit))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<? extends [Callable](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Callable.html)\<T\>\> tasks, long timeout, [TimeUnit](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/TimeUnit.html) 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. |
| abstract boolean | [isShutdown](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutorService.html#isShutdown())() Returns `true` if this executor has been shut down. |
| abstract boolean | [isTerminated](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutorService.html#isTerminated())() Returns `true` if all tasks have completed following shut down. |
| abstract void | [shutdown](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutorService.html#shutdown())() Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. |
| abstract [List](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/List.html)\<[Runnable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Runnable.html)\> | [shutdownNow](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutorService.html#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. |
| abstract \<T\> [Future](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html)\<T\> | [submit](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutorService.html#submit(java.util.concurrent.Callable<T>))([Callable](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Callable.html)\<T\> task) Submits a value-returning task for execution and returns a Future representing the pending results of the task. |
| abstract \<T\> [Future](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html)\<T\> | [submit](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutorService.html#submit(java.lang.Runnable,%20T))([Runnable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Runnable.html) task, T result) Submits a Runnable task for execution and returns a Future representing that task. |
| abstract [Future](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html)\<?\> | [submit](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutorService.html#submit(java.lang.Runnable))([Runnable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Runnable.html) task) Submits a Runnable task for execution and returns a Future representing that task. |

From interface [java.util.concurrent.Executor](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Executor.html)

|---|---|
| abstract void | [execute](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Executor.html#execute(java.lang.Runnable))([Runnable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Runnable.html) command) Executes the given command at some time in the future. |

## Public Methods

#### public abstract [ScheduledFuture](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ScheduledFuture.html)\<V\>
**schedule**
([Callable](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Callable.html)\<V\> callable, long delay, [TimeUnit](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/TimeUnit.html) unit)

Creates and executes a ScheduledFuture that becomes enabled after the
given delay.

##### Parameters

| callable | the function to execute |
| delay | the time from now to delay execution |
| unit | the time unit of the delay parameter |
|---|---|

##### Returns

- a ScheduledFuture that can be used to extract result or cancel

##### Throws

| [RejectedExecutionException](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/RejectedExecutionException.html) | if the task cannot be scheduled for execution |
| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if callable is null |
|---|---|

#### public abstract [ScheduledFuture](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ScheduledFuture.html)\<?\>
**schedule**
([Runnable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Runnable.html) command, long delay, [TimeUnit](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/TimeUnit.html) unit)

Creates and executes a one-shot action that becomes enabled
after the given delay.

##### Parameters

| command | the task to execute |
| delay | the time from now to delay execution |
| unit | the time unit of the delay parameter |
|---|---|

##### Returns

- a ScheduledFuture representing pending completion of the task and whose `get()` method will return `null` upon completion

##### Throws

| [RejectedExecutionException](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/RejectedExecutionException.html) | if the task cannot be scheduled for execution |
| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if command is null |
|---|---|

#### public abstract [ScheduledFuture](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ScheduledFuture.html)\<?\>
**scheduleAtFixedRate**
([Runnable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Runnable.html) command, long initialDelay, long period, [TimeUnit](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/TimeUnit.html) unit)

Creates and executes 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.

The sequence of task executions continues indefinitely until
one of the following exceptional completions occur:

- The task is {@linkplain Future#cancel explicitly cancelled} via the returned future.
- The executor terminates, also resulting in task cancellation.
- An execution of the task throws an exception. In this case calling `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html#get()` on the returned future will throw `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutionException.html`.

Subsequent executions are suppressed. Subsequent calls to `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html#isDone()` on the returned future will return `true`.

If any execution of this task takes longer than its period, then
subsequent executions may start late, but will not concurrently
execute.

##### Parameters

| command | the task to execute |
| initialDelay | the time to delay first execution |
| period | the period between successive executions |
| unit | the time unit of the initialDelay and period parameters |
|---|---|

##### Returns

- a ScheduledFuture representing pending completion of the series of repeated tasks. The future's `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html#get()` method will never return normally, and will throw an exception upon task cancellation or abnormal termination of a task execution.

##### Throws

| [RejectedExecutionException](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/RejectedExecutionException.html) | if the task cannot be scheduled for execution |
| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if command is null |
| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | if period less than or equal to zero |
|---|---|

#### public abstract [ScheduledFuture](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ScheduledFuture.html)\<?\>
**scheduleWithFixedDelay**
([Runnable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Runnable.html) command, long initialDelay, long delay, [TimeUnit](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/TimeUnit.html) unit)

Creates and executes 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.

The sequence of task executions continues indefinitely until
one of the following exceptional completions occur:

- The task is {@linkplain Future#cancel explicitly cancelled} via the returned future.
- The executor terminates, also resulting in task cancellation.
- An execution of the task throws an exception. In this case calling `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html#get()` on the returned future will throw `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutionException.html`.

Subsequent executions are suppressed. Subsequent calls to `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html#isDone()` on the returned future will return `true`.

<br />

##### Parameters

| command | the task to execute |
| initialDelay | the time to delay first execution |
| delay | the delay between the termination of one execution and the commencement of the next |
| unit | the time unit of the initialDelay and delay parameters |
|---|---|

##### Returns

- a ScheduledFuture representing pending completion of the series of repeated tasks. The future's `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html#get()` method will never return normally, and will throw an exception upon task cancellation or abnormal termination of a task execution.

##### Throws

| [RejectedExecutionException](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/RejectedExecutionException.html) | if the task cannot be scheduled for execution |
| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if command is null |
| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | if delay less than or equal to zero |
|---|---|