By default, an Observable and the chain of operators that you apply to it will do its work, and will notify its observers, on the same thread on which its
Subscribe
method is called. TheSubscribeOn
operator changes this behavior by specifying a different Scheduler on which the Observable should operate. TheObserveOn
operator specifies a different Scheduler that the Observable will use to send notifications to its observers.
As shown in this illustration, the
SubscribeOn
operator designates which thread the Observable will begin operating on, no matter at what point in the chain of operators that operator is called.ObserveOn
, on the other hand, affects the thread that the Observable will use below where that operator appears. For this reason, you may callObserveOn
multiple times at various points during the chain of Observable operators in order to change on which threads certain of those operators operate.
Schedulers.newThread
This scheduler simply starts a new thread every time it is requested via subscribeOn()
or observeOn()
.
Observable.just("Hello") .observeOn(Schedulers.newThread()) .doOnNext(s -> System.out.println(Thread.currentThread().getName()) ) .observeOn(Schedulers.newThread()) .subscribe(s -> { System.out.println(s); System.out.println(Thread.currentThread().getName()); } );
Schedulers.immediate
Schedulers.immediate
is a special scheduler that invokes a task within the client thread in a blocking way, rather than asynchronously and returns when the action is completed.In fact, subscribing to an Observable via immediate Scheduler typically has the same effect as not subscribing with any particular Scheduler at all.
observable.subscribeOn(Schedulers.immediate())
Schedulers.trampoline
It is quite similar to ImmediateScheduler as it also blocks the thread, however, it waits for the current task to execute completely(while Immediate Scheduler invokes the task right away). Trampoline schedulers come in handy when we have more than one observable and we want them to execute in order.
observable.subscribeOn(Schedulers.trampoline())
Schedulers.io
This Scheduler is similar to the newThread except for the fact that already started threads are recycled and can possibly handle future requests.
Observable.just("Hello") .observeOn(Schedulers.io()) .doOnNext(s -> System.out.println(Thread.currentThread().getName()) ) .subscribeOn(Schedulers.io()) .subscribe(s -> { System.out.println(s); System.out.println(Thread.currentThread().getName()); } );
Schedulers.from
there is a wrapper that can turn Executor into Scheduler using the from factory method
Executor executor = Executors.newSingleThreadExecutor(); Observable.just("Hello") .observeOn(Schedulers.from(executor)) .doOnNext(s -> System.out.println(Thread.currentThread().getName()) ) .subscribe(s -> { System.out.println(s); System.out.println(Thread.currentThread().getName()); } );
Schedulers.single
This scheduler is quite simple as it is backed just by one single thread. So no matter how many observables are there, it will run only on that one thread. It can be thought as a replacement to your main thread.
observable.subscribeOn(Schedulers.single())
Schedulers.computation
This scheduler is quite similar to IO Schedulers as this is backed by thread pool too. However, the number of threads that can be used is fixed to the number of cores present in the system.
observable.subscribeOn(Schedulers.computation())
Android Scheduler
This Scheduler is provided by rxAndroid library. This is used to bring back the execution to the main thread so that UI modification can be made. This is usually used in observeOn
method.
AndroidSchedulers.mainThread()
Observable.just("one", "two", "three", "four", "five") .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(/* an Observer */);
Observing on arbitrary loopers
Looper backgroundLooper = // ... Observable.just("one", "two", "three", "four", "five") .observeOn(AndroidSchedulers.from(backgroundLooper)) .subscribe(/* an Observer */)
References
http://reactivex.io/documentation/scheduler.html
https://android.jlelse.eu/rxjava-schedulers-what-when-and-how-to-use-it-6cfc27293add
https://www.baeldung.com/rxjava-schedulers