Use cases for RxJava schedulers

前端 未结 3 932
鱼传尺愫
鱼传尺愫 2020-11-28 00:12

In RxJava there are 5 different schedulers to choose from:

  1. immediate(): Creates and returns a Scheduler that executes work

3条回答
  •  情话喂你
    2020-11-28 01:02

    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. 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

提交回复
热议问题