public abstract static class Scheduler.Worker extends Object implements Disposable
Runnable
tasks on
an underlying task-execution scheme (such as custom Threads, event loop, Executor
or Actor system).
Disposing the Scheduler.Worker
should cancel all outstanding work and allows resource cleanup.
The default implementations of schedule(Runnable)
and schedulePeriodically(Runnable, long, long, TimeUnit)
delegate to the abstract schedule(Runnable, long, TimeUnit)
method. Its implementation is encouraged to
track the individual Runnable
tasks while they are waiting to be executed (with or without delay) so that
Disposable.dispose()
can prevent their execution or potentially interrupt them if they are currently running.
The default implementation of the now(TimeUnit)
method returns current
System.currentTimeMillis()
value in the desired time unit. Custom Worker
implementations can override this
to provide specialized time accounting (such as virtual time to be advanced programmatically).
Note that operators requiring a scheduler may rely on either of the now()
calls provided by
Scheduler
or Worker
respectively, therefore, it is recommended they represent a logically
consistent source of the current time.
The default implementation of the schedulePeriodically(Runnable, long, long, TimeUnit)
method uses
the schedule(Runnable, long, TimeUnit)
for scheduling the Runnable
task periodically.
The algorithm calculates the next absolute time when the task should run again and schedules this execution
based on the relative time between it and now(TimeUnit)
. However, drifts or changes in the
system clock would affect this calculation either by scheduling subsequent runs too frequently or too far apart.
Therefore, the default implementation uses the Scheduler.clockDriftTolerance()
value (set via
rx3.scheduler.drift-tolerance
in minutes) to detect a drift in now(TimeUnit)
and
re-adjust the absolute/relative time calculation accordingly.
If the Worker
is disposed, the schedule
methods
should return the Disposables.disposed()
singleton instance indicating the disposed
state to the caller. Since the Disposable.dispose()
call can happen on any thread, the schedule
implementations
should make best effort to cancel tasks immediately after those tasks have been submitted to the
underlying task-execution scheme if the dispose was detected after this submission.
All methods on the Worker
class should be thread safe.
Constructor and Description |
---|
Worker() |
Modifier and Type | Method and Description |
---|---|
long |
now(TimeUnit unit)
Returns the 'current time' of the Worker in the specified time unit.
|
Disposable |
schedule(Runnable run)
Schedules a Runnable for execution without any time delay.
|
abstract Disposable |
schedule(Runnable run,
long delay,
TimeUnit unit)
Schedules an Runnable for execution at some point in the future specified by a time delay
relative to the current time.
|
Disposable |
schedulePeriodically(Runnable run,
long initialDelay,
long period,
TimeUnit unit)
Schedules a periodic execution of the given task with the given initial time delay and repeat period.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
dispose, isDisposed
@NonNull public Disposable schedule(@NonNull Runnable run)
The default implementation delegates to schedule(Runnable, long, TimeUnit)
.
run
- Runnable to schedule@NonNull public abstract Disposable schedule(@NonNull Runnable run, long delay, @NonNull TimeUnit unit)
Note to implementors: non-positive delayTime
should be regarded as non-delayed schedule, i.e.,
as if the schedule(Runnable)
was called.
run
- the Runnable to scheduledelay
- time to "wait" before executing the action; non-positive values indicate an non-delayed
scheduleunit
- the time unit of delayTime
@NonNull public Disposable schedulePeriodically(@NonNull Runnable run, long initialDelay, long period, @NonNull TimeUnit unit)
The default implementation schedules and reschedules the Runnable
task via the
schedule(Runnable, long, TimeUnit)
method over and over and at a fixed rate, that is, the first execution will be after the
initialDelay
, the second after initialDelay + period
, the third after
initialDelay + 2 * period
, and so on.
Note to implementors: non-positive initialTime
and period
should be regarded as
non-delayed scheduling of the first and any subsequent executions.
In addition, a more specific Worker
implementation should override this method
if it can perform the periodic task execution with less overhead (such as by avoiding the
creation of the wrapper and tracker objects upon each periodic invocation of the
common schedule(Runnable, long, TimeUnit)
method).
run
- the Runnable to execute periodicallyinitialDelay
- time to wait before executing the action for the first time; non-positive values indicate
an non-delayed scheduleperiod
- the time interval to wait each time in between executing the action; non-positive values
indicate no delay between repeated schedulesunit
- the time unit of period