Schedulers in Rxjava android tutorial

 

If you want to introduce multithreading into your cascade of Observable operators, you can do so by instructing those operators (or particular Observables) to operate on particular Schedulers.

Check Prevoius Posts

  1. Rxjava Introduction
  2. Reactive Hello world application in android

  3. Transformation using RxJava tutorial for beginners

  4. Introducing Operators in RxJava tutorial for beginners

  5. Timer using Rxjava in android Interval operator

 

Schedulers give the opportunity to specify where and likely when to execute tasks related to the operation of an Observable chain.

Some ReactiveX Observable operators have variants that take a Scheduler as a parameter. These instruct the operator to do some or all of its work on a particular Scheduler.

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 Subscribe method is called.
                                                      The SubscribeOn operator changes this behavior by specifying a different Scheduler on which the Observable should operate. The ObserveOnoperator 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.
ObserveOn and SubscribeOn

There are two methods through which we can introduce Schedulers into our chain of operations:

  • subscribeOn: It specify which Scheduler invokes the code contained in Observable.create().
  • observeOn: It allows control to which Scheduler executes the code in the downstream operators.

RxJava provides some general use Schedulers:

  1. Schedulers.computation() : Used for CPU intensive tasks.
  2. Schedulers.io(): Used for IO bound tasks.
  3. Schedulers.from(Executor): Use with custom ExecutorService.
  4. Schedulers.newThread(): It always creates a new thread when a worker is needed. Since it’s not thread pooled and always creates a new thread instead of reusing one, this scheduler is not very useful.

Check Prevoius Posts

  1. Rxjava Introduction
  2. Reactive Hello world application in android

  3. Transformation using RxJava tutorial for beginners

  4. Introducing Operators in RxJava tutorial for beginners

  5. Timer using Rxjava in android Interval operator

 

 

 

Categories: RxJava2

Leave a Reply

Your email address will not be published. Required fields are marked *

Social Media Auto Publish Powered By : XYZScripts.com