Understand RxJava2’s SubscribeOn with Examples
RxJava is a Java implementation of ReactiveExtensions (or ReactiveX) which supports writing programs in asynchronous and event-based style through observable streams. SubscribeOn is one of the most-used operators in Rx so it is important to know how it works. This article assumes that you’re already familiar with basic RxJava’s concepts. Now let’s dive in!
What is SubscribeOn?
As the official doc has pointed out:
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
Subscribemethod is called. The SubscribeOn operator changes this behaviour by specifying a different Scheduler on which the Observable should operate. The ObserveOn 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 call ObserveOn multiple times at various points during the chain of Observable operators in order to change on which threads certain of those operators operate.
For the sake of simplicity, let’s leave out ObserveOn for now. The main differences between these two operators are:
- SubscribeOn changes the thread which the source operates on and ObserveOn changes the thread which the observers listen to.
- The source can operate in only one thread but the observers can listen to different threads.
How does SubscribeOn work in reality?
Let’s understand this operator by going through the definition step by step.
First of all, SubscribeOn will change the thread that the source operates on:
Also, if we don’t observe on another thread or switch to another data stream, the thread will be used for the rest of the chain. In the example, Just, a simple stream’s creation operator, emits provided items and from DoOnNext operator, the observation starts. All of them run on the same thread specified by SubscribeOn.
Secondly, the source will operate on a single thread, which means no matter how many SubscribeOn operators are used in the chain, only the first one takes effect:
Unless you use ObserveOn, you cannot alter the thread from now on.
A common use case that we usually have to deal with when using RxJava is combining multiple streams together:
When this occurs, the thread will be changed if the next source sets a new scheduler in SubscribeOn. You can trigger another source by utilising some operators such as FlatMap, AndThen, etc. Remember that in cases when we construct a new source in operators expecting a function or an interface, we are still in the previous thread. And of course, if you try to use SubscribeOn later on, it won’t have any effect.
What happens if we don’t subscribe to a new thread when switching sources? Let’s take a look at this example:
If we don’t specify a scheduler for the new source to run on, it will automatically use the previous thread and incoming SubscribeOn inevitably becomes an unnecessary line of code.
That’s it! Hopefully the article can make your life easier while working with RxJava. Happy coding :)