RxJava subscribeOn and ObserveOn
SubscribeOn and ObserveOn, two concepts that eludes most Rx beginners . I too belonged to this before my experiments using them. Below is a snippet of code that has been used by me to gain deeper understanding about this.
I am emitting an empty string and the map is used to print the thread in which the method was called. The output of the logs can be seen as comment next to the code.

ObserveOn works only downstream. All the methods following the observeOn have been moved to the IO thread. While the methods prior to the observeOn are still in the main thread.

While subscribeOn changes the thread for the calls prior to it and also the methods that follow it.

When multiple subscribeOns are used in succession, only the first one takes effect. The rest are rendered useless. The output of the code tells the same story. The first subscribeOn changes the thread to IO thread and though the subsequent subscribeOn tries to change the thread to computation thread, it is rendered obsolete by the subscribeOn preceding it.

On the contrary observeOn is able to change threads in subsequent calls as evident from the output of the logs.

As our final experiment we decided to use both subscribeOn and ObserveOn in tandem. The subscribeOn works upstream so the first map is in computation thread. The first observeOn was able to change the thread to IO thread and the second observeOn changed it to Main thread. The final subscribeOn is unable to change the thread set by observeOn.
This leads us to the following conclusions
- observeOn works only downstream.
- subscribeOn works downstream and upstream.
- consecutive subscribeOns do not change the thread.
- consequent observeOns do change the thread.
- Thread change by an observeOn cannot be overridden by a subscribeOn.