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 Object implements Disposable
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 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.

  • 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 - if run is null
    • 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 the schedule(Runnable) was called.

      Parameters:
      run - the Runnable to schedule
      delay - time to "wait" before executing the action; non-positive values indicate an non-delayed schedule
      unit - the time unit of delayTime
      Returns:
      a Disposable to be able to unsubscribe the action (cancel it if not executed)
      Throws:
      NullPointerException - if run or unit is null
    • 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 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).

      Parameters:
      run - the Runnable to execute periodically
      initialDelay - time to wait before executing the action for the first time; non-positive values indicate an non-delayed schedule
      period - the time interval to wait each time in between executing the action; non-positive values indicate no delay between repeated schedules
      unit - the time unit of period
      Returns:
      a Disposable to be able to unsubscribe the action (cancel it if not executed)
      Throws:
      NullPointerException - if run or unit is null
    • now

      public long now(@NonNull @NonNull TimeUnit unit)
      Returns the 'current time' of the Worker in the specified time unit.
      Parameters:
      unit - the time unit
      Returns:
      the 'current time'
      Throws:
      NullPointerException - if unit is null
      Since:
      2.0