tareq3
3/1/2019 - 1:28 PM

RxAndroid

RxAndroid {Scheduler added in rxAndroid }

RxAndroid is specific to Android Platform with few added classes on top of RxJava.

More specifically, Schedulers are introduced in RxAndroid (AndroidSchedulers.mainThread()) which plays major role in supporting multithreading concept in android applications.

Schedulers basically decides the thread on which a particular code runs whether on background thread or main thread. Apart from it everything we use is from RxJava library only.

Even through there are lot of Schedulers available,

*Schedulers.io() and *AndroidSchedulers.mainThread()

are extensively used in android programming. Below are the list of schedulers available and their brief introduction.

* Schedulers.io() –

This is used to perform non CPU-intensive operations like making network calls, reading disc / files, database operations etc., This maintains pool of threads.

* AndroidSchedulers.mainThread() –

This provides access to android Main Thread / UI Thread. Usually operations like updating UI, user interactions happens on this thread. We shouldn’t perform any intensive operations on this thread as it makes the app glitchy or ANR dialog can be thrown.

Schedulers.newThread() –

Using this, a new thread will be created each time a task is scheduled. It’s usually suggested not to use schedular unless there is a very long running operation. The threads created via newThread() won’t be reused.

Schedulers.computation() –

This schedular can be used to perform CPU-intensive operations like processing huge data, bitmap processing etc., The number of threads created using this scheduler completely depends on number CPU cores available.

Schedulers.single() –

This scheduler will execute all the tasks in sequential order they are added. This can be used when there is necessity of sequential execution is required.

Schedulers.immediate() –

This scheduler executes the the task immediately in synchronous way by blocking the main thread.

Schedulers.trampoline() –

It executes the tasks in First In – First Out manner. All the scheduled tasks will be executed one by one by limiting the number of background threads to one.

Schedulers.from() –

This allows us to create a scheduler from an executor by limiting number of threads to be created. When thread pool is occupied, tasks will be queued. Now we have the basic concepts needed. Let’s start with some key concepts of RxJava that everyone should aware of.