rxJava调度程序用例 [英] rxJava Schedulers Use Cases
问题描述
在RxJava中有 5种不同的调度程序可供选择:
In RxJava there are 5 different schedulers to choose from:
-
immediate():创建并返回一个在当前线程上立即执行工作的Scheduler 。
immediate(): Creates and returns a Scheduler that executes work immediately on the current thread.
trampoline():创建并返回一个调度程序,该调度程序在当前工作完成后要执行的当前线程上排队工作。
trampoline(): Creates and returns a Scheduler that queues work on the current thread to be executed after the current work completes.
newThread():创建并返回一个调度程序,为每个工作单元创建一个新线程。
newThread(): Creates and returns a Scheduler that creates a new Thread for each unit of work.
computation():创建并返回用于计算工作的调度程序。这可以用于事件循环,处理回调和其他计算工作。不要在此调度程序上执行IO绑定工作。使用调度程序。 io()代替。
computation(): Creates and returns a Scheduler intended for computational work. This can be used for event-loops, processing callbacks and other computational work. Do not perform IO-bound work on this scheduler. Use Schedulers.io() instead.
io():创建并返回预定的调度程序对于IO限制的工作。
该实现由Executor线程池支持,该线程池将根据需要增长。这可用于异步执行阻塞IO。不要在此调度程序上执行计算工作。使用调度程序。计算()代替。
io(): Creates and returns a Scheduler intended for IO-bound work. The implementation is backed by an Executor thread-pool that will grow as needed. This can be used for asynchronously performing blocking IO. Do not perform computational work on this scheduler. Use Schedulers.computation() instead.
问题:
前3个调度程序非常自我解释;但是,我对计算和 io 感到有点困惑。
- 什么究竟是IO约束工作?它是用于处理流(
java.io
)和文件(java.nio.files
)?它用于数据库查询吗?它是用于下载文件还是访问REST API? - 计算()与 newThread()有什么不同?所有计算()调用是每次都在单个(后台)线程而不是新(后台)线程吗?
- 为什么它对于在进行IO工作时调用计算()?
- 为什么在进行计算工作时调用 io()会不好?
- What exactly is "IO-bound work"? Is it used for dealing with streams (
java.io
) and files (java.nio.files
)? Is it used for database queries? Is it used for downloading files or accessing REST APIs? - How is computation() different from newThread()? Is it that all computation() calls are on a single (background) thread instead of a new (background) thread each time?
- Why is it bad to call computation() when doing IO work?
- Why is it bad to call io() when doing computational work?
推荐答案
很棒的问题,我认为文档可以提供更多细节。
Great questions, I think the documentation could do with some more detail.
-
io()
由无限制的线程池支持,这就是你要做的事情。用于非计算密集型任务,即不会对CPU造成太大负担的任务。因此,与文件系统的交互,与不同主机上的数据库或服务的交互就是很好的例子。 -
computation()
由有限的线程池支持,其大小等于可用处理器的数量。如果你试图在多个可用处理器之间并行安排cpu密集型工作(比如使用newThread()
),那么你就需要线程创建开销和上下文切换开销作为线程争夺处理器并且它可能会有很大的性能损失。 - 最好只为CPU密集型工作留下
计算()
将无法获得良好的CPU利用率。 - 由于2中讨论的原因,为计算工作调用
io()
是不好的。io()
是无限制的,如果你在io()
上并行安排了一千个计算任务,那么这千个任务中的每一个都是将各自拥有自己的线程并争夺CPU产生的上下文切换成本。
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.
这篇关于rxJava调度程序用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!