RxJava:Schedulers选择 newThread vs io
2016-01-13 18:11
471 查看
http://stackoverflow.com/questions/31276164/rxjava-schedulers-use-cases
一般的网络请求应该使用io,因为io使用了无限的线程池,而newThread没有线程池维护
immediate(): Creates and returns a Scheduler that executes work immediately on the current thread.
trampoline(): Creates and returns a Scheduler that queues work on the current thread to be executed after the current work completes.
newThread(): Creates and returns a Scheduler that creates a new Thread for each unit of work.
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(): 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.
Questions:
The first 3 schedulers are pretty self explanatory; however, I’m a little confused about computation and 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() 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 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.
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 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.
一般的网络请求应该使用io,因为io使用了无限的线程池,而newThread没有线程池维护
immediate(): Creates and returns a Scheduler that executes work immediately on the current thread.
trampoline(): Creates and returns a Scheduler that queues work on the current thread to be executed after the current work completes.
newThread(): Creates and returns a Scheduler that creates a new Thread for each unit of work.
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(): 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.
Questions:
The first 3 schedulers are pretty self explanatory; however, I’m a little confused about computation and 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() 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 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.
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 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.
相关文章推荐
- Android:在eclipse中的快捷键
- Myeclipse安装spket插件字体太小的解决方法
- java实现定时任务的三种方法
- java语言中除数为零问题
- Java-高精度
- spring启动component-scan类扫描加载过程---源码分析
- java.io.NotSerializableException: java.util.ArrayList$SubList
- 扩展Spring PropertyPlaceholderConfigurer实现根据环境加载配置文件
- 一个简单的SpringMVC需要哪些jar包[Spring4]
- 在java下使用log4j2记录日志
- java的property配置文件的用法
- @produces在spring mvc中是什么意思
- MyBatis整合Spring
- 三句话总结jdk与cglib代理的特点
- 详细解释Spring与Hibernate的整合
- Java安全应用——Bouncy Castle Crypto API
- CXF3.0.2+Spring3.2.14 WebService入门实例四
- OC里面的点语法与java中点调用的区别
- Hibernate 映射类型与Java 类型对照
- Elasticsearch java api 基本搜索部分详解