Schedulers
Observables are agnostic in terms of thread scheduling–in a multithreading environment, this is the job of a scheduler. Some of the operators presented variants that can take a scheduler as a parameter. There are specific calls that allow observing the flow either from downstream (the point where the operator is used, this is the case of observeOn
) or irrespective of call position (the call position does not matter, as this is the case of the subscribeOn
method). In the following example, we will print the current thread from upstream and downstream. Notice that in the case of subscribeOn
, the thread is always the same:
Notice the thread main usage from the map
method:
Notice that the thread main is no longer used from the map
method.
RxJava 2.0 offers more schedulers available from the io.reactivex.schedulers.Schedulers
factory, each one serving a specific purpose:
computation()
: Returns aScheduler
instance intended for computational workio()
: Returns aScheduler
instance intended...