RxJava - Schedulers

RxJava - Schedulers

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

Schedulers are used in multi-threading environment to work with Observable operators

By default, an Observable and the chain of operators that we apply to it will do its work, and will notify its observers, on the same thread on which its Subscribe method is called. The SubscribeOn operator changes this behavior 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

Below are the different types of Schedulers in RxJava

Sr.No. Scheduler & Description
1

Schedulers.computation()

Creates and returns a Scheduler intended for computational work. Count of threads to be scheduled depends upon the CPUs present in the system. One thread is allowed per CPU. Best for event-loops or callback operations.

2

Schedulers.io()

Creates and returns a Scheduler intended for IO-bound work. Thread pool may extend as needed.

3

Schedulers.newThread()

Creates and returns a Scheduler that creates a new Thread for each unit of work.

4

Schedulers.trampoline()

Creates and returns a Scheduler that queues work on the current thread to be executed after the current work completes.

5

Schedulers.from(java.util.concurrent.Executor executor)

Converts an Executor into a new Scheduler instance.