0

I'm learning RxJava and I have some doubts regarding the Scheduler. When to use which one. The AndroidSchedulers.mainThread() is quite easy and clear, whenever we need to come to the Android UI thread, we can use this scheduler. But I'm confused with the other Schedulers

  • Schedulers.io()
  • Schedulers.computation()

  • Schedulers.newThread()

In many samples I have seen pepople using Schedulers.io() and Schedulers.computation() for network calls , db operations etc. Can we pick any of them randomly for background tasks? If not which are the suitable situations to pick each? When to use Schedulers.newThread()? It would be helpful if someone could explain it in simple words. Thanks in advance!!

Emanuel
  • 8,027
  • 2
  • 37
  • 56
  • 1
    Each method on `Schedulers` have a detailed Javadoc that suggest usages: http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/schedulers/Schedulers.html – akarnokd Oct 12 '17 at 08:00
  • using `newThread()` for every background operation is fine? –  Oct 12 '17 at 08:06
  • If you don't mind creating a lots of threads then yes. Otherwise, `io()` does reuse threads and is more economic. – akarnokd Oct 12 '17 at 08:30
  • The `io` and `computation` will return the same instances all the time? If many streams have to be executed in `io` it will queue the tasks? –  Oct 12 '17 at 08:33
  • So, if you have an api call in `io` and after completion you are saving it to local db , and then passing to UI , if you make api call and db write in `io` it will first complete network call , then will complete db write , and then only will come to UI? Instead if we use a `newThread` for db write , will it execute the db write parellally with the UI , So that UI does not have to wait for db write to complete? –  Oct 12 '17 at 08:36
  • There are dozens of blogs explaining schedulers and the related flow operators. Please check them out first. Schedulers don't work the way you described. Whenever an operator needs a thread, it picks a worker from the provided scheduler, which then will utilize the same underlying thread as long as the flow is running. The difference between the schedulers is that how they manage their set of backing threads. – akarnokd Oct 12 '17 at 08:46
  • If possible can you suggest any link where we can understand it properly? Many blogs I read including the official doc, but those are either hard to digest for a beginner like me, or they just tell it is just changing threads. They don't explain in detail about how it works internally –  Oct 12 '17 at 08:51
  • If you are not experienced with concurrency and ExecutorServices, then it may not really help you; this series describes [Schedulers](http://akarnokd.blogspot.hu/2015/05/schedulers-part-1.html) and this describes [the operators](http://akarnokd.blogspot.hu/2016/03/subscribeon-and-observeon.html). – akarnokd Oct 12 '17 at 08:59

1 Answers1

0

io(): When you want to perform I/O bound work like database requests, network calls etc. use io. This scheduler is backed by an unbounded thread pool(which may cause OutOfMemory error if you go crazy with number of threads you are using). What it essentially means is that you don't get the overhead of creating a new thread every time. See more Why is creating thread expensive?

newThread(): As the name implies, it will create a new thread every single time.

computation: If you have some expensive CPU bound operation, you should use this Scheduler. This scheduler is bounded i.e there a limited number of threads based on the system.

As other have pointed out, please take a look at official documentation to understand more here

Dhruv Jagetiya
  • 1,043
  • 12
  • 19