Class Scheduler.Worker
- java.lang.Object
-
- io.reactivex.rxjava3.core.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
public abstract static class Scheduler.Worker extends java.lang.Object implements Disposable
Represents an isolated, sequential worker of a parent Scheduler for executingRunnabletasks on an underlying task-execution scheme (such as custom Threads, event loop,Executoror Actor system).Disposing the
Scheduler.Workershould cancel all outstanding work and allows resource cleanup.The default implementations of
schedule(Runnable)andschedulePeriodically(Runnable, long, long, TimeUnit)delegate to the abstractschedule(Runnable, long, TimeUnit)method. Its implementation is encouraged to track the individualRunnabletasks while they are waiting to be executed (with or without delay) so thatDisposable.dispose()can prevent their execution or potentially interrupt them if they are currently running.The default implementation of the
now(TimeUnit)method returns currentSystem.currentTimeMillis()value in the desired time unit, unlessrx3.scheduler.use-nanotime(boolean) is set. When the property is set totrue, the method usesSystem.nanoTime()as its basis instead. CustomWorkerimplementations 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 thenow()calls provided bySchedulerorWorkerrespectively, 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 theschedule(Runnable, long, TimeUnit)for scheduling theRunnabletask 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 andnow(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 theScheduler.clockDriftTolerance()value (set viarx3.scheduler.drift-toleranceandrx3.scheduler.drift-tolerance-unit) to detect a drift innow(TimeUnit)and re-adjust the absolute/relative time calculation accordingly.If the
Workeris disposed, theschedulemethods should return theDisposable.disposed()singleton instance indicating the disposed state to the caller. Since theDisposable.dispose()call can happen on any thread, thescheduleimplementations 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
Workerclass should be thread safe.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) classScheduler.Worker.PeriodicTaskHolds state and logic to calculate when the next delayed invocation of this task has to happen (accounting for clock drifts).
-
Constructor Summary
Constructors Constructor Description Worker()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description longnow(@NonNull java.util.concurrent.TimeUnit unit)Returns the 'current time' of the Worker in the specified time unit.@NonNull Disposableschedule(@NonNull java.lang.Runnable run)Schedules a Runnable for execution without any time delay.abstract @NonNull Disposableschedule(@NonNull java.lang.Runnable run, long delay, @NonNull java.util.concurrent.TimeUnit unit)Schedules an Runnable for execution at some point in the future specified by a time delay relative to the current time.@NonNull DisposableschedulePeriodically(@NonNull java.lang.Runnable run, long initialDelay, long period, @NonNull java.util.concurrent.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
-
-
-
-
Method Detail
-
schedule
@NonNull public @NonNull Disposable schedule(@NonNull @NonNull java.lang.Runnable run)
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:
java.lang.NullPointerException- ifrunisnull
-
schedule
@NonNull public abstract @NonNull Disposable schedule(@NonNull @NonNull java.lang.Runnable run, long delay, @NonNull @NonNull java.util.concurrent.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
delayTimeshould 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:
java.lang.NullPointerException- ifrunorunitisnull
-
schedulePeriodically
@NonNull public @NonNull Disposable schedulePeriodically(@NonNull @NonNull java.lang.Runnable run, long initialDelay, long period, @NonNull @NonNull java.util.concurrent.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
Runnabletask 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
initialTimeandperiodshould be regarded as non-delayed scheduling of the first and any subsequent executions. In addition, a more specificWorkerimplementation 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:
java.lang.NullPointerException- ifrunorunitisnull
-
-