Use cases for RxJava schedulers
The most important point is that both Schedulers.io and Schedulers.computation are backed by unbounded thread pools as opposed to the others mentioned in the question. This characteristic is only shared by the Schedulers.from(Executor) in the case the Executor is created with newCachedThreadPool (unbounded with an auto-reclaim thread pool).
As abundantly explained in previous responses and multiple articles on the web, Schedulers.io and Schedulers.computation shall be used carefully as they are optimized for the type of work in their name. But, to my point of view, they're most important role is to provide real concurrency to reactive streams.
Contrary to newcomers belief, reactive streams are not inherently concurrent but inherently asynchronous and sequential. For this very reason, Schedulers.io shall be used only when the I/O operation is blocking (eg: using a blocking command such as Apache IOUtils FileUtils.readFileAsString(...)) thus would freeze the calling thread until the operation is done.
Using an asynchronous method such as Java AsynchronousFileChannel(...) wouldn't block the calling thread during the operation so there is no point in using a separate thread. In fact, Schedulers.io threads are not really a good fit for asynchronous operations as they don't run an event loop and the callback would never... be called.
The same logic applies for database access or remote API calls. Don't use the Schedulers.io if you can use an asynchronous or reactive API to make the call.
Back to concurrency. You may not have access to an async or reactive API to do I/O operations asynchronously or concurrently, so your only alternative is to dispatch multiple calls on a separate thread. Alas, Reactive streams are sequential at their ends but the good news is that the flatMap() operator can introduce concurrency at their core.
Concurrency must be built in the stream construct, typically using the flatMap() operator. This powerful operator can be configured to internally provide a multi-threaded context to your flatMap() embedded Function<T, R>. That context is provided by a multi-threaded Scheduler such as Scheduler.io or Scheduler.computation.
Find more details in articles on RxJava2 Schedulers and Concurrency where you'll find code sample and detailed explanations on how to use Schedulers sequentially and concurrently.
Hope this helps,
Softjake
Great questions, I think the documentation could do with some more detail.
io()
is backed by an unbounded thread-pool and is the sort of thing you'd use for non-computationally intensive tasks, that is stuff that doesn't put much load on the CPU. So yep interaction with the file system, interaction with databases or services on a different host are good examples.computation()
is backed by a bounded thread-pool with size equal to the number of available processors. If you tried to schedule CPU intensive work in parallel across more than the available processors (say usingnewThread()
) then you are up for thread creation overhead and context switching overhead as threads vie for a processor and it's potentially a big performance hit.- It's best to leave
computation()
for CPU intensive work only otherwise you won't get good CPU utilization. - It's bad to call
io()
for computational work for the reason discussed in 2.io()
is unbounded and if you schedule a thousand computational tasks onio()
in parallel then each of those thousand tasks will each have their own thread and be competing for CPU incurring context switching costs.