Top Description Inners Fields Constructors Methods
rx.internal.schedulers

public Class SchedulerWhen

extends Scheduler
implements Subscription
Class Inheritance
All Implemented Interfaces
rx.Subscription
Imports
java.util.concurrent.TimeUnit, java.util.concurrent.atomic.AtomicBoolean, .AtomicReference, rx.Completable, .Completable.OnSubscribe, .CompletableSubscriber, .Observable, .Observer, .Scheduler, .Subscription, rx.functions.Action0, .Func1, rx.internal.operators.BufferUntilSubscriber, rx.observers.SerializedObserver, rx.subjects.PublishSubject, rx.subscriptions.Subscriptions

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 rx.Scheduler.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 rx.Scheduler.Workers rather than individual actions. Generally each Observable uses its own rx.Scheduler.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));
    }));
});
Since
1.3

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
pack-priv static class
pack-priv static class
pack-priv abstract static class

Field Summary

Modifier and TypeField and Description
private final Scheduler
pack-priv static final Subscription
private final Subscription
pack-priv static final Subscription
private final Observer<Observable<Completable>>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Scheduler.Worker
createWorker()

Implements abstract rx.Scheduler.createWorker.

Retrieves or creates a new Scheduler.Worker that represents serial execution of actions.
public boolean
isUnsubscribed()

Implements rx.Subscription.isUnsubscribed.

Indicates whether this Subscription is currently unsubscribed.
public void
unsubscribe()

Implements rx.Subscription.unsubscribe.

Stops the receipt of notifications on the Subscriber that was registered when this Subscription was received.
Inherited from rx.Scheduler:
nowwhen

Field Detail

actualSchedulerback to summary
private final Scheduler actualScheduler
SUBSCRIBEDback to summary
pack-priv static final Subscription SUBSCRIBED
subscriptionback to summary
private final Subscription subscription
UNSUBSCRIBEDback to summary
pack-priv static final Subscription UNSUBSCRIBED
workerObserverback to summary
private final Observer<Observable<Completable>> workerObserver

Constructor Detail

SchedulerWhenback to summary
public SchedulerWhen(Func1<Observable<Observable<Completable>>, Completable> combine, Scheduler actualScheduler)

Method Detail

createWorkerback to summary
public Scheduler.Worker createWorker()

Implements abstract rx.Scheduler.createWorker.

Doc from rx.Scheduler.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

Annotations
@Override
isUnsubscribedback to summary
public boolean isUnsubscribed()

Implements rx.Subscription.isUnsubscribed.

Doc from rx.Subscription.isUnsubscribed.

Indicates whether this Subscription is currently unsubscribed.

Returns:boolean

true if this Subscription is currently unsubscribed, false otherwise

Annotations
@Override
unsubscribeback to summary
public void unsubscribe()

Implements rx.Subscription.unsubscribe.

Doc from rx.Subscription.unsubscribe.

Stops the receipt of notifications on the Subscriber that was registered when this Subscription was received.

This allows deregistering an Subscriber before it has finished receiving all events (i.e. before onCompleted is called).

Annotations
@Override
rx.internal.schedulers back to summary

pack-priv Class SchedulerWhen.DelayedAction

extends ScheduledAction
Class Inheritance
Annotations
@SuppressWarnings:serial

Field Summary

Modifier and TypeField and Description
private final Action0
private final long
private final TimeUnit

Constructor Summary

AccessConstructor and Description
public
DelayedAction(Action0 action, long delayTime, TimeUnit unit)

Method Summary

Modifier and TypeMethod and Description
protected Subscription
Inherited from rx.internal.schedulers.SchedulerWhen.ScheduledAction:
isUnsubscribedunsubscribe

Field Detail

actionback to summary
private final Action0 action
delayTimeback to summary
private final long delayTime
unitback to summary
private final TimeUnit unit

Constructor Detail

DelayedActionback to summary
public DelayedAction(Action0 action, long delayTime, TimeUnit unit)

Method Detail

callActualback to summary
protected Subscription callActual(Scheduler.Worker actualWorker, CompletableSubscriber actionCompletable)

Implements abstract rx.internal.schedulers.SchedulerWhen.ScheduledAction.callActual.

Annotations
@Override
rx.internal.schedulers back to summary

pack-priv Class SchedulerWhen.ImmediateAction

extends ScheduledAction
Class Inheritance
Annotations
@SuppressWarnings:serial

Field Summary

Modifier and TypeField and Description
private final Action0

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected Subscription
Inherited from rx.internal.schedulers.SchedulerWhen.ScheduledAction:
isUnsubscribedunsubscribe

Field Detail

actionback to summary
private final Action0 action

Constructor Detail

ImmediateActionback to summary
public ImmediateAction(Action0 action)

Method Detail

callActualback to summary
protected Subscription callActual(Scheduler.Worker actualWorker, CompletableSubscriber actionCompletable)

Implements abstract rx.internal.schedulers.SchedulerWhen.ScheduledAction.callActual.

Annotations
@Override
rx.internal.schedulers back to summary

pack-priv Class SchedulerWhen.OnCompletedAction

extends Object
implements Action0
Class Inheritance
All Implemented Interfaces
rx.functions.Action0, rx.functions.Action, rx.functions.Function

Field Summary

Modifier and TypeField and Description
private Action0
private CompletableSubscriber

Constructor Summary

AccessConstructor and Description
public
OnCompletedAction(Action0 action, CompletableSubscriber actionCompletable)

Method Summary

Modifier and TypeMethod and Description
public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

actionback to summary
private Action0 action
actionCompletableback to summary
private CompletableSubscriber actionCompletable

Constructor Detail

OnCompletedActionback to summary
public OnCompletedAction(Action0 action, CompletableSubscriber actionCompletable)

Method Detail

callback to summary
public void call()

Implements rx.functions.Action0.call.

Annotations
@Override
rx.internal.schedulers back to summary

pack-priv abstract Class SchedulerWhen.ScheduledAction

extends AtomicReference<Subscription>
implements Subscription
Class Inheritance
All Implemented Interfaces
rx.Subscription
Known Direct Subclasses
rx.internal.schedulers.SchedulerWhen.ImmediateAction, rx.internal.schedulers.SchedulerWhen.DelayedAction
Annotations
@SuppressWarnings:serial

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private void
call(Scheduler.Worker actualWorker, CompletableSubscriber actionCompletable)

protected abstract Subscription
callActual(Scheduler.Worker actualWorker, CompletableSubscriber actionCompletable)

public boolean
isUnsubscribed()

Implements rx.Subscription.isUnsubscribed.

Indicates whether this Subscription is currently unsubscribed.
public void
unsubscribe()

Implements rx.Subscription.unsubscribe.

Stops the receipt of notifications on the Subscriber that was registered when this Subscription was received.
Inherited from java.util.concurrent.atomic.AtomicReference:
accumulateAndGetcompareAndExchangecompareAndExchangeAcquirecompareAndExchangeReleasecompareAndSetgetgetAcquiregetAndAccumulategetAndSetgetAndUpdategetOpaquegetPlainlazySetsetsetOpaquesetPlainsetReleasetoStringupdateAndGetweakCompareAndSetweakCompareAndSetAcquireweakCompareAndSetPlainweakCompareAndSetReleaseweakCompareAndSetVolatile

Constructor Detail

ScheduledActionback to summary
public ScheduledAction()

Method Detail

callback to summary
private void call(Scheduler.Worker actualWorker, CompletableSubscriber actionCompletable)
callActualback to summary
protected abstract Subscription callActual(Scheduler.Worker actualWorker, CompletableSubscriber actionCompletable)
isUnsubscribedback to summary
public boolean isUnsubscribed()

Implements rx.Subscription.isUnsubscribed.

Doc from rx.Subscription.isUnsubscribed.

Indicates whether this Subscription is currently unsubscribed.

Returns:boolean

true if this Subscription is currently unsubscribed, false otherwise

Annotations
@Override
unsubscribeback to summary
public void unsubscribe()

Implements rx.Subscription.unsubscribe.

Doc from rx.Subscription.unsubscribe.

Stops the receipt of notifications on the Subscriber that was registered when this Subscription was received.

This allows deregistering an Subscriber before it has finished receiving all events (i.e. before onCompleted is called).

Annotations
@Override