public abstract class

Scheduler

extends Object
java.lang.Object
   ↳ io.reactivex.Scheduler
Known Direct Subclasses

Class Overview

A Scheduler is an object that specifies an API for scheduling units of work with or without delays or periodically. You can get common instances of this class in Schedulers.

Summary

Nested Classes
class Scheduler.Worker Sequential Scheduler for executing actions on a single thread or event loop. 
Public Constructors
Scheduler()
Public Methods
static long clockDriftTolerance()
Returns the clock drift tolerance in nanoseconds.
abstract Scheduler.Worker createWorker()
Retrieves or creates a new Scheduler.Worker that represents serial execution of actions.
long now(TimeUnit unit)
Returns the 'current time' of the Scheduler in the specified time unit.
Disposable scheduleDirect(Runnable run)
Schedules the given task on this scheduler non-delayed execution.
Disposable scheduleDirect(Runnable run, long delay, TimeUnit unit)
Schedules the execution of the given task with the given delay amount.
Disposable schedulePeriodicallyDirect(Runnable run, long initialDelay, long period, TimeUnit unit)
Schedules a periodic execution of the given task with the given initial delay and period.
void shutdown()
Instructs the Scheduler instance to stop threads and stop accepting tasks on any outstanding Workers.
void start()
Allows the Scheduler instance to start threads and accept tasks on them.
<S extends Scheduler & Disposable> S when(Function<Flowable<Flowable<Completable>>, Completable> combine)
Allows the use of operators for controlling the timing around when actions scheduled on workers are actually done.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public Scheduler ()

Public Methods

public static long clockDriftTolerance ()

Returns the clock drift tolerance in nanoseconds.

Related system property: rx2.scheduler.drift-tolerance in minutes

Returns
  • the tolerance in nanoseconds

public abstract Scheduler.Worker createWorker ()

Retrieves or creates a new Scheduler.Worker that represents serial execution of actions.

When work is completed it should be unsubscribed using dispose().

Work on a Scheduler.Worker is guaranteed to be sequential.

Returns
  • a Worker representing a serial queue of actions to be executed

public long now (TimeUnit unit)

Returns the 'current time' of the Scheduler in the specified time unit.

Parameters
unit the time unit
Returns
  • the 'current time'

public Disposable scheduleDirect (Runnable run)

Schedules the given task on this scheduler non-delayed execution.

This method is safe to be called from multiple threads but there are no ordering guarantees between tasks.

Parameters
run the task to execute
Returns
  • the Disposable instance that let's one cancel this particular task.

public Disposable scheduleDirect (Runnable run, long delay, TimeUnit unit)

Schedules the execution of the given task with the given delay amount.

This method is safe to be called from multiple threads but there are no ordering guarantees between tasks.

Parameters
run the task to schedule
delay the delay amount, non-positive values indicate non-delayed scheduling
unit the unit of measure of the delay amount
Returns
  • the Disposable that let's one cancel this particular delayed task.

public Disposable schedulePeriodicallyDirect (Runnable run, long initialDelay, long period, TimeUnit unit)

Schedules a periodic execution of the given task with the given initial delay and period.

This method is safe to be called from multiple threads but there are no ordering guarantees between tasks.

The periodic execution is at a fixed rate, that is, the first execution will be after the initial delay, the second after initialDelay + period, the third after initialDelay + 2 * period, and so on.

Parameters
run the task to schedule
initialDelay the initial delay amount, non-positive values indicate non-delayed scheduling
period the period at which the task should be re-executed
unit the unit of measure of the delay amount
Returns
  • the Disposable that let's one cancel this particular delayed task.

public void shutdown ()

Instructs the Scheduler instance to stop threads and stop accepting tasks on any outstanding Workers.

Implementations should make sure the call is idempotent and thread-safe.

public void start ()

Allows the Scheduler instance to start threads and accept tasks on them.

Implementations should make sure the call is idempotent and thread-safe.

public S when (Function<Flowable<Flowable<Completable>>, Completable> combine)

Allows the use of operators for controlling the timing around when actions scheduled on workers are actually done. This makes it possible to layer additional behavior on this Scheduler. The only parameter is a function that flattens an Flowable of Flowable of Completables into just one Completable. There must be a chain of operators connecting the returned value to the source Flowable otherwise any work scheduled on the returned Scheduler will not be executed.

When createWorker() is invoked a Flowable of Completables is onNext'd to the combinator to be flattened. If the inner Flowable is not immediately subscribed to an calls to schedule(Runnable) are buffered. Once the Flowable is subscribed to actions are then onNext'd as Completables.

Finally the actions scheduled on the parent Scheduler when the inner most Completables are subscribed to.

When the Scheduler.Worker is unsubscribed the Completable emits an onComplete and triggers any behavior in the flattening operator. The Flowable and all Completables give to the flattening function never onError.

Limit the amount concurrency two at a time without creating a new fix size thread pool:

 Scheduler limitScheduler = Schedulers.computation().when(workers -> {
  // use merge max concurrent to limit the number of concurrent
  // callbacks two at a time
  return Completable.merge(Flowable.merge(workers), 2);
 });
 

This is a slightly different way to limit the concurrency but it has some interesting benefits and drawbacks to the method above. It works by limited the number of concurrent Scheduler.Workers rather than individual actions. Generally each Flowable uses its own Scheduler.Worker. This means that this will essentially limit the number of concurrent subscribes. The danger comes from using operators like zip(org.reactivestreams.Publisher, org.reactivestreams.Publisher, io.reactivex.functions.BiFunction) where subscribing to the first Flowable could deadlock the subscription to the second.

 Scheduler limitScheduler = Schedulers.computation().when(workers -> {
  // use merge max concurrent to limit the number of concurrent
  // Flowables two at a time
  return Completable.merge(Flowable.merge(workers, 2));
 });
 
Slowing down the rate to no more than than 1 a second. This suffers from the same problem as the one above I could find an Flowable operator that limits the rate without dropping the values (aka leaky bucket algorithm).
 Scheduler slowScheduler = Schedulers.computation().when(workers -> {
  // use concatenate to make each worker happen one at a time.
  return Completable.concat(workers.map(actions -> {
      // delay the starting of the next worker by 1 second.
      return Completable.merge(actions.delaySubscription(1, TimeUnit.SECONDS));
  }));
 });
 

History: 2.0.1 - experimental

Parameters
combine the function that takes a two-level nested Flowable sequence of a Completable and returns the Completable that will be subscribed to and should trigger the execution of the scheduled Actions.
Returns
  • the Scheduler with the customized execution behavior