Scheduler.Worker (RxJava Javadoc 3.1.10) (original) (raw)

Represents an isolated, sequential worker of a parent Scheduler for executing 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 thatDisposable.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 totrue, 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 byScheduler 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 viarx3.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.