Rxjava 简明教程
RxJava - Schedulers
在多线程环境中使用计划程序来使用 Observable 运算符。
Schedulers are used in multi-threading environment to work with Observable operators.
根据 Reactive ,计划程序用于计划运算符链应用到不同的线程的方式。
As per the Reactive,Scheduler are used to schedule how chain of operators will apply to different threads.
默认情况下,一个 Observable 及其应用到它的运算符链将在调用其 Subscribe 方法的同一线程上执行其工作,并通知其观察者。SubscribeOn 运算符通过指定 Observable 应该在它上操作的不同计划程序来更改此行为。ObserveOn 运算符指定 Observable 将使用它的观察者来发送通知的不同计划程序。
By default, an Observable and the chain of operators that you apply to it will do its work, and will notify its observers, on the same thread on which its Subscribe method is called. The SubscribeOn operator changes this behavior by specifying a different Scheduler on which the Observable should operate. The ObserveOn operator specifies a different Scheduler that the Observable will use to send notifications to its observers.
RxJava 中有以下类型的计划程序可用:
There are following types of Schedulers available in RxJava −
Sr.No. |
Scheduler & Description |
1 |
Schedulers.computation() Creates and returns a Scheduler intended for computational work. Count of threads to be scheduled depends upon the CPUs present in the system. One thread is allowed per CPU. Best for event-loops or callback operations. |
2 |
Schedulers.io() Creates and returns a Scheduler intended for IO-bound work. Thread pool may extend as needed. |
3 |
Schedulers.newThread() Creates and returns a Scheduler that creates a new Thread for each unit of work. |
4 |
Schedulers.trampoline() Creates and returns a Scheduler that queues work on the current thread to be executed after the current work completes. |
4 |
Schedulers.from(java.util.concurrent.Executor executor) Converts an Executor into a new Scheduler instance. |