Schedulers are the easiest way to bring multi-threading into your apps. They are an important part of RxJava and they play perfectly along with Observables. They provide a handy way to create concurrent routines without having to deal with implementations, synchronizations, threading, platform limitations, and platform changes.
RxJava offers five kinds of Schedulers:
.io()
.computation()
.immediate()
.newThread()
.trampoline()
Let's take a look at them one by one.
This Scheduler is meant for I/O operations. It's based on a thread pool that adjusts its size when necessary, growing or shrinking. This is the Scheduler we are going to use to fix the StrictMode
violation we saw previously. As it's specific for I/O, it's not the default for any RxJava method; it's up to the developer to use it properly.
It's important to note that with the thread pool unbounded, a large amount of scheduled I/O operations will create a large amount of threads and memory usage. As always, we have to look for the effective balance between performance and simplicity.
This is the default scheduler for every computational work that is not related to I/O. It's the default for a lot of RxJava methods: buffer()
, debounce()
, delay()
, interval()
, sample()
, and skip()
.
This Scheduler allows you to immediately start the specified work on the current thread. It's the default Scheduler for timeout()
, timeInterval()
, and timestamp()
.
18.224.54.168