Top Description Inners Constructors Methods
rx

public abstract Class Scheduler

extends Object
Class Inheritance
Known Direct Subclasses
rx.internal.schedulers.CachedThreadScheduler, rx.internal.schedulers.EventLoopsScheduler, rx.internal.schedulers.ExecutorScheduler, rx.internal.schedulers.ImmediateScheduler, rx.internal.schedulers.NewThreadScheduler, rx.internal.schedulers.SchedulerWhen, rx.internal.schedulers.TrampolineScheduler, rx.schedulers.ImmediateScheduler, rx.schedulers.NewThreadScheduler, rx.schedulers.TestScheduler, rx.schedulers.TrampolineScheduler
Imports
java.util.concurrent.TimeUnit, rx.functions.*, rx.internal.schedulers.*, rx.schedulers.Schedulers

A Scheduler is an object that schedules units of work. You can find common implementations of this class in Schedulers.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public abstract static class
Scheduler.Worker

Sequential Scheduler for executing actions on a single thread or event loop.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public abstract Scheduler.Worker

Returns:

a Worker representing a serial queue of actions to be executed
createWorker
()

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

public long

Returns:

the scheduler's notion of current absolute time in milliseconds
now
()

Gets the current time, in milliseconds, according to this Scheduler.

public <
a Scheduler and a Subscription
S extends Scheduler & Subscription
>
S

Returns:

the Scheduler with the customized execution behavior
when
(Func1<Observable<Observable<Completable>>, Completable>
the function that takes a two-level nested Observable sequence of a Completable and returns the Completable that will be subscribed to and should trigger the execution of the scheduled Actions.
combine
)

Allows the use of operators for controlling the timing around when actions scheduled on workers are actually done.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Schedulerback to summary
public Scheduler()

Method Detail

createWorkerback to summary
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 Scheduler.Worker#unsubscribe().

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

Returns:Scheduler.Worker

a Worker representing a serial queue of actions to be executed

nowback to summary
public long now()

Gets the current time, in milliseconds, according to this Scheduler.

Returns:long

the scheduler's notion of current absolute time in milliseconds

whenback to summary
public <S extends Scheduler & Subscription> S when(Func1<Observable<Observable<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 Observable of Observable of Completables into just one Completable. There must be a chain of operators connecting the returned value to the source Observable otherwise any work scheduled on the returned Scheduler will not be executed.

When Scheduler#createWorker() is invoked a Observable of Completables is onNext'd to the combinator to be flattened. If the inner Observable is not immediately subscribed to an calls to Worker#schedule are buffered. Once the Observable 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 Worker is unsubscribed the Completable emits an onComplete and triggers any behavior in the flattening operator. The Observable 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(Observable.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 Workers rather than individual actions. Generally each Observable uses its own Worker. This means that this will essentially limit the number of concurrent subscribes. The danger comes from using operators like Observable#zip(Observable, Observable, rx.functions.Func2) where subscribing to the first Observable could deadlock the subscription to the second.

Scheduler limitScheduler = Schedulers.computation().when(workers -> {
    // use merge max concurrent to limit the number of concurrent
    // Observables two at a time
    return Completable.merge(Observable.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 Observable 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));
   }));
});
Parameters
<S>
a Scheduler and a Subscription
combine:Func1<Observable<Observable<Completable>>, Completable>

the function that takes a two-level nested Observable sequence of a Completable and returns the Completable that will be subscribed to and should trigger the execution of the scheduled Actions.

Returns:S

the Scheduler with the customized execution behavior

Annotations
@SuppressWarnings:unchecked
Since
1.3
rx back to summary

public abstract Class Scheduler.Worker

extends Object
implements Subscription
Class Inheritance
All Implemented Interfaces
rx.Subscription
Known Direct Subclasses
rx.internal.schedulers.CachedThreadScheduler.EventLoopWorker, rx.internal.schedulers.EventLoopsScheduler.EventLoopWorker, rx.internal.schedulers.ExecutorScheduler.ExecutorSchedulerWorker, rx.internal.schedulers.ImmediateScheduler.InnerImmediateScheduler, rx.internal.schedulers.NewThreadWorker, rx.internal.schedulers.TrampolineScheduler.InnerCurrentThreadScheduler, rx.schedulers.TestScheduler.InnerTestScheduler

Sequential Scheduler for executing actions on a single thread or event loop.

Unsubscribing the Worker cancels all outstanding work and allows resources cleanup.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public long

Returns:

the scheduler's notion of current absolute time in milliseconds
now
()

Gets the current time, in milliseconds, according to this Scheduler.

public abstract Subscription

Returns:

a subscription to be able to prevent or cancel the execution of the action
schedule
(Action0
Action to schedule
action
)

Schedules an Action for execution.

public abstract Subscription

Returns:

a subscription to be able to prevent or cancel the execution of the action
schedule
(final Action0
the Action to schedule
action
,
final long
time to wait before executing the action; non-positive values indicate an non-delayed schedule
delayTime
,
final TimeUnit
the time unit of delayTime
unit
)

Schedules an Action for execution at some point in the future.

public Subscription

Returns:

a subscription to be able to prevent or cancel the execution of the action
schedulePeriodically
(final Action0
the Action to execute periodically
action
,
long
time to wait before executing the action for the first time; non-positive values indicate an non-delayed schedule
initialDelay
,
long
the time interval to wait each time in between executing the action; non-positive values indicate no delay between repeated schedules
period
,
TimeUnit
the time unit of period
unit
)

Schedules a cancelable action to be executed periodically.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Workerback to summary
public Worker()

Method Detail

nowback to summary
public long now()

Gets the current time, in milliseconds, according to this Scheduler.

Returns:long

the scheduler's notion of current absolute time in milliseconds

scheduleback to summary
public abstract Subscription schedule(Action0 action)

Schedules an Action for execution.

Parameters
action:Action0

Action to schedule

Returns:Subscription

a subscription to be able to prevent or cancel the execution of the action

scheduleback to summary
public abstract Subscription schedule(final Action0 action, final long delayTime, final TimeUnit unit)

Schedules an Action for execution at some point in the future.

Note to implementors: non-positive delayTime should be regarded as non-delayed schedule, i.e., as if the schedule(rx.functions.Action0) was called.

Parameters
action:Action0

the Action to schedule

delayTime:long

time to wait before executing the action; non-positive values indicate an non-delayed schedule

unit:TimeUnit

the time unit of delayTime

Returns:Subscription

a subscription to be able to prevent or cancel the execution of the action

schedulePeriodicallyback to summary
public Subscription schedulePeriodically(final Action0 action, long initialDelay, long period, TimeUnit unit)

Schedules a cancelable action to be executed periodically. This default implementation schedules recursively and waits for actions to complete (instead of potentially executing long-running actions concurrently). Each scheduler that can do periodic scheduling in a better way should override this.

Note to implementors: non-positive initialTime and period should be regarded as non-delayed scheduling of the first and any subsequent executions.

Parameters
action:Action0

the Action to execute periodically

initialDelay:long

time to wait before executing the action for the first time; non-positive values indicate an non-delayed schedule

period:long

the time interval to wait each time in between executing the action; non-positive values indicate no delay between repeated schedules

unit:TimeUnit

the time unit of period

Returns:Subscription

a subscription to be able to prevent or cancel the execution of the action