简体   繁体   English

RxJava/RxAndroid 中的 Schedulers.computation() 与 Schedulers.io()

[英]Schedulers.computation() v/s Schedulers.io() in RxJava/RxAndroid

I have read through so many answers on SO like the following use-cases-for-rxjava-schedulers , what-is-the-difference-between-schedulers-io-and-schedulers-computation , rxjava2-schedulers-io-vs-schedulers-computation-with-large-concurrent-request .我已经阅读了很多关于 SO 的答案,例如以下use-cases-for-rxjava-schedulerswhat-is-the-difference-between-schedulers-io-and-schedulers-computationrxjava2-schedulers-io-vs- schedulers-computation-with-large-concurrent-request

The most common explanation is use Schedulers.computation() for CPU intensive work最常见的解释是使用Schedulers.computation() 进行 CPU 密集型工作

and use Schedulers.io() for interaction with the file system, interaction with databases or services, REST API calls并使用Schedulers.io() 与文件系统交互,与数据库或服务交互,REST API 调用

By CPU intensive work I am assuming/considering Image Resizing/operations, Large Data sets, etc. (please add some other CPU intensive tasks if you know any which are normally performed on an Android App)通过 CPU 密集型工作,我假设/考虑图像调整大小/操作、大数据集等(如果您知道通常在 Android 应用程序上执行的任务,请添加一些其他 CPU 密集型任务)

My question is我的问题是

  1. what qualifies as a large data set in Android? Android 中的大数据集是什么? (tangible sense) (有形的感觉)
  2. If.network calls or queries to the database respond with the huge data set(as per question 1) then what?如果对数据库的网络调用或查询以庞大的数据集(根据问题 1)响应,那么会怎样?

In fact, they just use a different thread pool and therefore need to be used for their intended purpose.事实上,它们只是使用不同的线程池,因此需要用于它们的预期目的。

For data processing, you need to use a Schedulers.computation() , and for data input and output Schedulers.io()对于数据处理,您需要使用Schedulers.computation() ,对于数据输入和 output Schedulers.io()

This is done to limit the creation of infinitely new threads and thus to create a queue of jobs.这样做是为了限制无限新线程的创建,从而创建作业队列。

From the documentation of rx :rx 的文档

Schedulers.computation( ) - meant for computational work such as event-loops and callback processing; Schedulers.computation( ) - 用于计算工作,例如事件循环和回调处理; do not use this scheduler for I/O (use Schedulers.io( ) instead);不要将此调度程序用于 I/O(改为使用 Schedulers.io( )); the number of threads, by default, is equal to the number of processors默认情况下,线程数等于处理器数

Schedulers.io( ) - meant for I/O-bound work such as asynchronous performance of blocking I/O, this scheduler is backed by a thread-pool that will grow as needed; Schedulers.io( ) - 用于 I/O 绑定工作,例如阻塞 I/O 的异步性能,这个调度器由一个线程池支持,该线程池将根据需要增长; for ordinary computational work, switch to Schedulers.computation( );对于普通的计算工作,切换到 Schedulers.computation( ); Schedulers.io( ) by default is a CachedThreadScheduler, which is something like a new thread scheduler with thread caching Schedulers.io( ) 默认是一个 CachedThreadScheduler,它类似于一个新的线程调度器,带有线程缓存

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM