Class Scheduler.Worker
- All Implemented Interfaces:
Disposable
- Direct Known Subclasses:
ComputationScheduler.EventLoopWorker
,ExecutorScheduler.ExecutorWorker
,ImmediateThinScheduler.ImmediateThinWorker
,IoScheduler.EventLoopWorker
,NewThreadWorker
,SchedulerWhen.QueueWorker
,SingleScheduler.ScheduledWorker
,TestScheduler.TestWorker
,TrampolineScheduler.TrampolineWorker
- Enclosing class:
Scheduler
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, unless rx3.scheduler.use-nanotime
(boolean) is set. When the property is set to
true
, the method uses System.nanoTime()
as its basis instead. 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
and rx3.scheduler.drift-tolerance-unit
) 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 Disposable.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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) final class
Holds state and logic to calculate when the next delayed invocation of this task has to happen (accounting for clock drifts). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlong
Returns the 'current time' of the Worker in the specified time unit.Schedules a Runnable for execution without any time delay.abstract @NonNull Disposable
Schedules an Runnable for execution at some point in the future specified by a time delay relative to the current time.schedulePeriodically
(@NonNull Runnable run, long initialDelay, long period, @NonNull TimeUnit unit) Schedules a periodic execution of the given task with the given initial time delay and repeat period.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.reactivex.rxjava3.disposables.Disposable
dispose, isDisposed
-
Constructor Details
-
Worker
public Worker()
-
-
Method Details
-
schedule
Schedules a Runnable for execution without any time delay.The default implementation delegates to
schedule(Runnable, long, TimeUnit)
.- Parameters:
run
- Runnable to schedule- Returns:
- a Disposable to be able to unsubscribe the action (cancel it if not executed)
- Throws:
NullPointerException
- ifrun
isnull
-
schedule
@NonNull public abstract @NonNull Disposable schedule(@NonNull @NonNull Runnable run, long delay, @NonNull @NonNull TimeUnit unit) Schedules an Runnable for execution at some point in the future specified by a time delay relative to the current time.Note to implementors: non-positive
delayTime
should be regarded as non-delayed schedule, i.e., as if theschedule(Runnable)
was called.- Parameters:
run
- the Runnable to scheduledelay
- time to "wait" before executing the action; non-positive values indicate an non-delayed scheduleunit
- the time unit ofdelayTime
- Returns:
- a Disposable to be able to unsubscribe the action (cancel it if not executed)
- Throws:
NullPointerException
- ifrun
orunit
isnull
-
schedulePeriodically
@NonNull public @NonNull Disposable schedulePeriodically(@NonNull @NonNull Runnable run, long initialDelay, long period, @NonNull @NonNull TimeUnit unit) Schedules a periodic execution of the given task with the given initial time delay and repeat period.The default implementation schedules and reschedules the
Runnable
task via theschedule(Runnable, long, TimeUnit)
method over and over and at a fixed rate, that is, the first execution will be after theinitialDelay
, the second afterinitialDelay + period
, the third afterinitialDelay + 2 * period
, and so on.Note to implementors: non-positive
initialTime
andperiod
should be regarded as non-delayed scheduling of the first and any subsequent executions. In addition, a more specificWorker
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 commonschedule(Runnable, long, TimeUnit)
method).- Parameters:
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 ofperiod
- Returns:
- a Disposable to be able to unsubscribe the action (cancel it if not executed)
- Throws:
NullPointerException
- ifrun
orunit
isnull
-
now
Returns the 'current time' of the Worker in the specified time unit.- Parameters:
unit
- the time unit- Returns:
- the 'current time'
- Throws:
NullPointerException
- ifunit
isnull
- Since:
- 2.0
-