Top Description Inners Fields Constructors Methods
io.reactivex.rxjava3.internal.schedulers

public Class SchedulerWhen

extends Scheduler
implements Disposable
Class Inheritance
All Implemented Interfaces
io.reactivex.rxjava3.disposables.Disposable
Imports
java.util.concurrent.TimeUnit, java.util.concurrent.atomic.*, io.reactivex.rxjava3.annotations.NonNull, io.reactivex.rxjava3.core.*, io.reactivex.rxjava3.disposables.*, io.reactivex.rxjava3.functions.Function, io.reactivex.rxjava3.internal.util.ExceptionHelper, io.reactivex.rxjava3.processors.*

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 Flowable#zip(org.reactivestreams.Publisher, org.reactivestreams.Publisher, io.reactivex.rxjava3.functions.BiFunction) 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));
 }));
});

History 2.0.1 - experimental

Since
2.1

Nested and Inner Type Summary

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

Field Summary

Modifier and TypeField and Description
private final Scheduler
private Disposable
pack-priv static final Disposable
pack-priv static final Disposable
private final FlowableProcessor<Flowable<Completable>>

Constructor Summary

AccessConstructor and Description
public

Method Summary

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

Implements abstract io.reactivex.rxjava3.core.Scheduler.createWorker.

Retrieves or creates a new Scheduler.Worker that represents sequential execution of actions.
public void
dispose()

Implements io.reactivex.rxjava3.disposables.Disposable.dispose.

Dispose the resource, the operation should be idempotent.
public boolean
isDisposed()

Implements io.reactivex.rxjava3.disposables.Disposable.isDisposed.

Returns true if this resource has been disposed.
Inherited from io.reactivex.rxjava3.core.Scheduler:
clockDriftTolerancenowscheduleDirectscheduleDirectschedulePeriodicallyDirectshutdownstartwhen

Field Detail

actualSchedulerback to summary
private final Scheduler actualScheduler
disposableback to summary
private Disposable disposable
DISPOSEDback to summary
pack-priv static final Disposable DISPOSED
SUBSCRIBEDback to summary
pack-priv static final Disposable SUBSCRIBED
workerProcessorback to summary
private final FlowableProcessor<Flowable<Completable>> workerProcessor

Constructor Detail

SchedulerWhenback to summary
public SchedulerWhen(Function<Flowable<Flowable<Completable>>, Completable> combine, Scheduler actualScheduler)

Method Detail

createWorkerback to summary
public @NonNull Scheduler.Worker createWorker()

Implements abstract io.reactivex.rxjava3.core.Scheduler.createWorker.

Doc from io.reactivex.rxjava3.core.Scheduler.createWorker.

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

When work is completed, the Worker instance should be released by calling Scheduler.Worker#dispose() to avoid potential resource leaks in the underlying task-execution scheme.

Work on a Scheduler.Worker is guaranteed to be sequential and non-overlapping.

Returns:@NonNull Scheduler.Worker

a Worker representing a serial queue of actions to be executed

Annotations
@NonNull
@Override
disposeback to summary
public void dispose()

Implements io.reactivex.rxjava3.disposables.Disposable.dispose.

Doc from io.reactivex.rxjava3.disposables.Disposable.dispose.

Dispose the resource, the operation should be idempotent.

Annotations
@Override
isDisposedback to summary
public boolean isDisposed()

Implements io.reactivex.rxjava3.disposables.Disposable.isDisposed.

Doc from io.reactivex.rxjava3.disposables.Disposable.isDisposed.

Returns true if this resource has been disposed.

Returns:boolean

true if this resource has been disposed

Annotations
@Override
io.reactivex.rxjava3.internal.schedulers back to summary

pack-priv final Class SchedulerWhen.CreateWorkerFunction

extends Object
implements Function<SchedulerWhen.ScheduledAction, Completable>
Class Inheritance
  • java.lang.Object
  • io.reactivex.rxjava3.internal.schedulers.SchedulerWhen.CreateWorkerFunction
All Implemented Interfaces
io.reactivex.rxjava3.functions.Function

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv class

Field Summary

Modifier and TypeField and Description
pack-priv final Scheduler.Worker

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public Completable
apply(final SchedulerWhen.ScheduledAction
the input value
action
)

Implements io.reactivex.rxjava3.functions.Function.apply.

Apply some calculation to the input value and return some other value.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

actualWorkerback to summary
pack-priv final Scheduler.Worker actualWorker

Constructor Detail

CreateWorkerFunctionback to summary
pack-priv CreateWorkerFunction(Scheduler.Worker actualWorker)

Method Detail

applyback to summary
public Completable apply(final SchedulerWhen.ScheduledAction action)

Implements io.reactivex.rxjava3.functions.Function.apply.

Doc from io.reactivex.rxjava3.functions.Function.apply.

Apply some calculation to the input value and return some other value.

Parameters
action:SchedulerWhen.ScheduledAction

the input value

Returns:Completable

the output value

Annotations
@Override
io.reactivex.rxjava3.internal.schedulers back to summary

pack-priv final Class SchedulerWhen.CreateWorkerFunction.WorkerCompletable

extends Completable
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final SchedulerWhen.ScheduledAction

Constructor Summary

AccessConstructor and Description
pack-priv

Field Detail

actionback to summary
pack-priv final SchedulerWhen.ScheduledAction action

Constructor Detail

WorkerCompletableback to summary
pack-priv WorkerCompletable(SchedulerWhen.ScheduledAction action)

Method Detail

subscribeActualback to summary
protected void subscribeActual(CompletableObserver actionCompletable)
Annotations
@Override
io.reactivex.rxjava3.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 Runnable
private final long
private final TimeUnit

Constructor Summary

AccessConstructor and Description
pack-priv
DelayedAction(Runnable action, long delayTime, TimeUnit unit)

Method Summary

Modifier and TypeMethod and Description
protected Disposable
Inherited from io.reactivex.rxjava3.internal.schedulers.SchedulerWhen.ScheduledAction:
calldisposeisDisposed

Field Detail

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

Constructor Detail

DelayedActionback to summary
pack-priv DelayedAction(Runnable action, long delayTime, TimeUnit unit)

Method Detail

callActualback to summary
protected Disposable callActual(Scheduler.Worker actualWorker, CompletableObserver actionCompletable)

Implements abstract io.reactivex.rxjava3.internal.schedulers.SchedulerWhen.ScheduledAction.callActual.

Annotations
@Override
io.reactivex.rxjava3.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 Runnable

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
protected Disposable
Inherited from io.reactivex.rxjava3.internal.schedulers.SchedulerWhen.ScheduledAction:
calldisposeisDisposed

Field Detail

actionback to summary
private final Runnable action

Constructor Detail

ImmediateActionback to summary
pack-priv ImmediateAction(Runnable action)

Method Detail

callActualback to summary
protected Disposable callActual(Scheduler.Worker actualWorker, CompletableObserver actionCompletable)

Implements abstract io.reactivex.rxjava3.internal.schedulers.SchedulerWhen.ScheduledAction.callActual.

Annotations
@Override
io.reactivex.rxjava3.internal.schedulers back to summary

pack-priv Class SchedulerWhen.OnCompletedAction

extends Object
implements Runnable
Class Inheritance
  • java.lang.Object
  • io.reactivex.rxjava3.internal.schedulers.SchedulerWhen.OnCompletedAction
All Implemented Interfaces
java.lang.Runnable

Field Summary

Modifier and TypeField and Description
pack-priv final Runnable
pack-priv final CompletableObserver

Constructor Summary

AccessConstructor and Description
pack-priv
OnCompletedAction(Runnable action, CompletableObserver actionCompletable)

Method Summary

Modifier and TypeMethod and Description
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

actionback to summary
pack-priv final Runnable action
actionCompletableback to summary
pack-priv final CompletableObserver actionCompletable

Constructor Detail

OnCompletedActionback to summary
pack-priv OnCompletedAction(Runnable action, CompletableObserver actionCompletable)

Method Detail

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override
io.reactivex.rxjava3.internal.schedulers back to summary

pack-priv final Class SchedulerWhen.QueueWorker

extends Worker
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final FlowableProcessor<SchedulerWhen.ScheduledAction>
private final Scheduler.Worker
private final AtomicBoolean

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
dispose()

Implements io.reactivex.rxjava3.disposables.Disposable.dispose.

Dispose the resource, the operation should be idempotent.
public boolean
isDisposed()

Implements io.reactivex.rxjava3.disposables.Disposable.isDisposed.

Returns true if this resource has been disposed.
public @NonNull Disposable
schedule(final @NonNull Runnable action, final long delayTime, final @NonNull TimeUnit unit)

public @NonNull Disposable
schedule(final @NonNull Runnable action)

Inherited from io.reactivex.rxjava3.core.Scheduler.Worker:
nowschedulescheduleschedulePeriodically

Field Detail

actionProcessorback to summary
private final FlowableProcessor<SchedulerWhen.ScheduledAction> actionProcessor
actualWorkerback to summary
private final Scheduler.Worker actualWorker
unsubscribedback to summary
private final AtomicBoolean unsubscribed

Constructor Detail

QueueWorkerback to summary
pack-priv QueueWorker(FlowableProcessor<SchedulerWhen.ScheduledAction> actionProcessor, Scheduler.Worker actualWorker)

Method Detail

disposeback to summary
public void dispose()

Implements io.reactivex.rxjava3.disposables.Disposable.dispose.

Doc from io.reactivex.rxjava3.disposables.Disposable.dispose.

Dispose the resource, the operation should be idempotent.

Annotations
@Override
isDisposedback to summary
public boolean isDisposed()

Implements io.reactivex.rxjava3.disposables.Disposable.isDisposed.

Doc from io.reactivex.rxjava3.disposables.Disposable.isDisposed.

Returns true if this resource has been disposed.

Returns:boolean

true if this resource has been disposed

Annotations
@Override
scheduleback to summary
public @NonNull Disposable schedule(final @NonNull Runnable action, final long delayTime, final @NonNull TimeUnit unit)
Annotations
@NonNull
@Override
scheduleback to summary
public @NonNull Disposable schedule(final @NonNull Runnable action)
Annotations
@NonNull
@Override
io.reactivex.rxjava3.internal.schedulers back to summary

pack-priv abstract Class SchedulerWhen.ScheduledAction

extends AtomicReference<Disposable>
implements Disposable
Class Inheritance
All Implemented Interfaces
io.reactivex.rxjava3.disposables.Disposable
Known Direct Subclasses
io.reactivex.rxjava3.internal.schedulers.SchedulerWhen.ImmediateAction, io.reactivex.rxjava3.internal.schedulers.SchedulerWhen.DelayedAction
Annotations
@SuppressWarnings:serial

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv void
call(Scheduler.Worker actualWorker, CompletableObserver actionCompletable)

protected abstract Disposable
callActual(Scheduler.Worker actualWorker, CompletableObserver actionCompletable)

public void
dispose()

Implements io.reactivex.rxjava3.disposables.Disposable.dispose.

Dispose the resource, the operation should be idempotent.
public boolean
isDisposed()

Implements io.reactivex.rxjava3.disposables.Disposable.isDisposed.

Returns true if this resource has been disposed.
Inherited from java.util.concurrent.atomic.AtomicReference:
accumulateAndGetcompareAndExchangecompareAndExchangeAcquirecompareAndExchangeReleasecompareAndSetgetgetAcquiregetAndAccumulategetAndSetgetAndUpdategetOpaquegetPlainlazySetsetsetOpaquesetPlainsetReleasetoStringupdateAndGetweakCompareAndSetweakCompareAndSetAcquireweakCompareAndSetPlainweakCompareAndSetReleaseweakCompareAndSetVolatile

Constructor Detail

ScheduledActionback to summary
pack-priv ScheduledAction()

Method Detail

callback to summary
pack-priv void call(Scheduler.Worker actualWorker, CompletableObserver actionCompletable)
callActualback to summary
protected abstract Disposable callActual(Scheduler.Worker actualWorker, CompletableObserver actionCompletable)
disposeback to summary
public void dispose()

Implements io.reactivex.rxjava3.disposables.Disposable.dispose.

Doc from io.reactivex.rxjava3.disposables.Disposable.dispose.

Dispose the resource, the operation should be idempotent.

Annotations
@Override
isDisposedback to summary
public boolean isDisposed()

Implements io.reactivex.rxjava3.disposables.Disposable.isDisposed.

Doc from io.reactivex.rxjava3.disposables.Disposable.isDisposed.

Returns true if this resource has been disposed.

Returns:boolean

true if this resource has been disposed

Annotations
@Override
io.reactivex.rxjava3.internal.schedulers back to summary

pack-priv final Class SchedulerWhen.SubscribedDisposable

extends Object
implements Disposable
Class Inheritance
  • java.lang.Object
  • io.reactivex.rxjava3.internal.schedulers.SchedulerWhen.SubscribedDisposable
All Implemented Interfaces
io.reactivex.rxjava3.disposables.Disposable

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
dispose()

Implements io.reactivex.rxjava3.disposables.Disposable.dispose.

Dispose the resource, the operation should be idempotent.
public boolean
isDisposed()

Implements io.reactivex.rxjava3.disposables.Disposable.isDisposed.

Returns true if this resource has been disposed.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

SubscribedDisposableback to summary
pack-priv SubscribedDisposable()

Method Detail

disposeback to summary
public void dispose()

Implements io.reactivex.rxjava3.disposables.Disposable.dispose.

Doc from io.reactivex.rxjava3.disposables.Disposable.dispose.

Dispose the resource, the operation should be idempotent.

Annotations
@Override
isDisposedback to summary
public boolean isDisposed()

Implements io.reactivex.rxjava3.disposables.Disposable.isDisposed.

Doc from io.reactivex.rxjava3.disposables.Disposable.isDisposed.

Returns true if this resource has been disposed.

Returns:boolean

true if this resource has been disposed

Annotations
@Override