本文介绍了RxJava调度程序的用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在RxJava中,有 5种不同的调度程序可供选择:

In RxJava there are 5 different schedulers to choose from:

  1. immediate():创建并返回一个计划程序,该计划程序立即在当前线程上执行工作.

  1. 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():创建并返回计划用于计算工作的Scheduler.这可以用于事件循环,处理回调和其他计算工作.不要在此调度程序上执行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绑定工作的Scheduler. 该实现由Executor线程池支持,该线程池将根据需要增长.这可用于异步执行阻塞IO.不要在此调度程序上执行计算工作.使用调度程序. computation().

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.

问题:

前三个调度程序很容易说明.但是,我对 computation io 感到困惑.

  1. "IO绑定工作"到底是什么?它是否用于处理流(java.io)和文件(java.nio.files)?它用于数据库查询吗?它用于下载文件或访问REST API吗?
  2. computation() newThread()有何不同?是否所有 computation()调用都在单个(后台)线程上,而不是每次都在一个新的(后台)线程上?
  3. 为什么在进行IO工作时调用 computation()很糟糕?
  4. 为什么在进行计算工作时调用 io()很糟糕?
  1. 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?
  2. 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?
  3. Why is it bad to call computation() when doing IO work?
  4. Why is it bad to call io() when doing computational work?

推荐答案

很好的问题,我认为文档可以提供更多细节.

Great questions, I think the documentation could do with some more detail.

  1. io()由无限制的线程池支持,是您用于非计算密集型任务的一种东西,即不会给CPU带来太大负载的东西.因此,与文件系统的交互,与不同主机上的数据库或服务的交互都是很好的例子.
  2. computation()由有界线程池支持,该线程池的大小等于可用处理器的数量.如果您试图跨可用处理器并行调度CPU密集型工作(例如使用newThread()),那么您将承担线程创建开销和上下文切换开销,因为线程争夺处理器,这可能会严重影响性能.
  3. 最好仅将computation()留作CPU密集型工作,否则您将无法获得良好的CPU利用率.
  4. 出于2中讨论的原因,调用io()进行计算工作很不好.io()是不受限制的,并且如果您在io()上并行调度一千个计算任务,那么这千个任务中的每一个都会有自己的任务线程并争夺CPU的上下文切换成本.
  1. 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.
  2. 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 using newThread()) 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.
  3. It's best to leave computation() for CPU intensive work only otherwise you won't get good CPU utilization.
  4. 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 on io() in parallel then each of those thousand tasks will each have their own thread and be competing for CPU incurring context switching costs.

这篇关于RxJava调度程序的用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 12:44