RxJava — Schedulers — What, when and how to use it?

Aanand Shekhar Roy
AndroidPub
Published in
4 min readFeb 1, 2018

--

Schedulers are one of the main components in RxJava. They are responsible for performing operations of Observable on different threads. They help to offload the time-consuming onto different threads. In this post we will learn the types of schedulers and when to use the different types.

  • IO — This is one of the most common types of Schedulers that are used. They are generally used for IO related stuff. Such as network requests, file system operations. IO scheduler is backed by thread-pool.

Java Thread pool represents a group of worker threads that are waiting for the job and reuse many times.

It starts with creating one worker, which can be re-used for other operations. Of course, if it can’t be re-used (in case of long processing jobs), it spawns a new thread (or worker) for handling operation. This benefit could also be problematic because it is unbounded and can have a drastic effect on overall performance if a huge number of threads are spawned (though the unused threads are removed after 60 seconds of inactivity). But still IO scheduler is a good scheduler to use. It is available as:

observable.subscribeOn(Schedulers.io())

  • Computation — This scheduler is quite similar to IO Schedulers as this is backed by thread pool too. However, the number of…

--

--

Aanand Shekhar Roy
AndroidPub

Senior Software Engineer @Joist, Author of Kotlin Programming Cookbook.