public interface Scheduler extends Disposable
Implementations that use an underlying ExecutorService
or
ScheduledExecutorService
should decorate it with the relevant Schedulers
hook
(Schedulers.decorateExecutorService(Scheduler, ScheduledExecutorService)
.
Modifier and Type | Interface and Description |
---|---|
static interface |
Scheduler.Worker
A worker representing an asynchronous boundary that executes tasks.
|
Disposable.Composite, Disposable.Swap
Modifier and Type | Method and Description |
---|---|
Scheduler.Worker |
createWorker()
Creates a worker of this Scheduler.
|
default void |
dispose()
Instructs this Scheduler to release all resources and reject
any new tasks to be executed.
|
default long |
now(TimeUnit unit)
Returns the "current time" notion of this scheduler.
|
Disposable |
schedule(Runnable task)
Schedules the non-delayed execution of the given task on this scheduler.
|
default Disposable |
schedule(Runnable task,
long delay,
TimeUnit unit)
Schedules the execution of the given task with the given delay amount.
|
default Disposable |
schedulePeriodically(Runnable task,
long initialDelay,
long period,
TimeUnit unit)
Schedules a periodic execution of the given task with the given initial delay and period.
|
default void |
start()
Instructs this Scheduler to prepare itself for running tasks
directly or through its Workers.
|
isDisposed
Scheduler.Worker createWorker()
Once the Worker is no longer in use, one should call dispose() on it to
release any resources the particular Scheduler may have used.
It depends on the implementation, but Scheduler Workers should usually run tasks in
FIFO order. Some implementations may entirely delegate the scheduling to an
underlying structure (like an ExecutorService
).
default void dispose()
The operation is thread-safe but one should avoid using start() and dispose() concurrently as it would non-deterministically leave the Scheduler in either active or inactive state.
The Scheduler may choose to ignore this instruction.
dispose
in interface Disposable
default long now(TimeUnit unit)
unit
- the target unit of the current timeDisposable schedule(Runnable task)
This method is safe to be called from multiple threads but there are no ordering guarantees between tasks.
task
- the task to executeDisposable
instance that let's one cancel this particular task.
If the Scheduler
has been shut down, throw a RejectedExecutionException
.default Disposable schedule(Runnable task, long delay, TimeUnit unit)
This method is safe to be called from multiple threads but there are no ordering guarantees between tasks.
task
- the task to scheduledelay
- the delay amount, non-positive values indicate non-delayed schedulingunit
- the unit of measure of the delay amountDisposable
that let's one cancel this particular delayed task,
or throw a RejectedExecutionException
if the Scheduler is not capable of scheduling periodically.default Disposable schedulePeriodically(Runnable task, long initialDelay, long period, TimeUnit unit)
This method is safe to be called from multiple threads but there are no ordering guarantees between tasks.
The periodic execution is at a fixed rate, that is, the first execution will be after the initial delay, the second after initialDelay + period, the third after initialDelay + 2 * period, and so on.
task
- the task to scheduleinitialDelay
- the initial delay amount, non-positive values indicate non-delayed schedulingperiod
- the period at which the task should be re-executedunit
- the unit of measure of the delay amountDisposable
that let's one cancel this particular delayed task,
or throw a RejectedExecutionException
if the Scheduler is not capable of scheduling periodically.default void start()
The operation is thread-safe but one should avoid using start() and dispose() concurrently as it would non-deterministically leave the Scheduler in either active or inactive state.