ReactiveX 官方文檔之調度器 Scheduler
如果你想給Observable操作符鏈添加多線程功能,你可以指定操作符(或者特定的Observable)在特定的調度器(Scheduler)上執行。
某些ReactiveX的Observable操作符有一些變體,它們可以接受一個Scheduler參數。這個參數指定操作符將它們的部分或全部任務放在一個特定的調度器上執行。
使用ObserveOn和SubscribeOn操作符,你可以讓Observable在一個特定的調度器上執行,ObserveOn指示一個 Observable在一個特定的調度器上調用觀察者的onNext, onError和onCompleted方法,SubscribeOn更進一步,它指示Observable將全部的處理過程(包括發射數據和通知)放在特定的調度器上執行。
RxJava示例
調度器的種類
下表展示了RxJava中可用的調度器種類:
調度器類型 | 效果 | </tr> </tbody>|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Schedulers.computation(?) | 用于計算任務,如事件循環或和回調處理,不要用于IO操作(IO操作請使用Schedulers.io());默認線程數等于處理器的數量 | </tr>|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Schedulers.from(executor) | 使用指定的Executor作為調度器 | </tr>|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Schedulers.immediate(?) | 在當前線程立即開始執行任務 | </tr>|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Schedulers.io(?) | 用于IO密集型任務,如異步阻塞IO操作,這個調度器的線程池會根據需要增長;對于普通的計算任務,請使用 Schedulers.computation();Schedulers.io(?)默認是一個CachedThreadScheduler,很像一個有線程緩存的新線程調度器 | </tr>|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Schedulers.newThread(?) | 為每個任務創建一個新線程 | </tr>|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Schedulers.trampoline(?) | 當其它排隊的任務完成后,在當前線程排隊開始執行 | </tr> </tbody> </table>
操作符 | 調度器 | </tr> </tbody>|||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
buffer(timespan) | computation | </tr>|||||||||||||||||||||||||
buffer(timespan,?count) | computation | </tr>|||||||||||||||||||||||||
buffer(timespan,?timeshift) | computation | </tr>|||||||||||||||||||||||||
debounce(timeout,?unit) | computation | </tr>|||||||||||||||||||||||||
delay(delay,?unit) | computation | </tr>|||||||||||||||||||||||||
delaySubscription(delay,?unit) | computation | </tr>|||||||||||||||||||||||||
interval | computation | </tr>|||||||||||||||||||||||||
repeat | trampoline | </tr>|||||||||||||||||||||||||
replay(time,?unit) | computation | </tr>|||||||||||||||||||||||||
replay(buffersize,?time,?unit) | computation | </tr>|||||||||||||||||||||||||
replay(selector,?time,?unit) | computation | </tr>|||||||||||||||||||||||||
replay(selector,?buffersize,?time,?unit) | computation | </tr>|||||||||||||||||||||||||
retry | trampoline | </tr>|||||||||||||||||||||||||
sample(period,?unit) | computation | </tr>|||||||||||||||||||||||||
skip(time,?unit) | computation | </tr>|||||||||||||||||||||||||
skipLast(time,?unit) | computation | </tr>|||||||||||||||||||||||||
take(time,?unit) | computation | </tr>|||||||||||||||||||||||||
takeLast(time,?unit) | computation | </tr>|||||||||||||||||||||||||
takeLast(count,?time,?unit) | computation | </tr>|||||||||||||||||||||||||
takeLastBuffer(time,?unit) | computation | </tr>|||||||||||||||||||||||||
takeLastBuffer(count,?time,?unit) | computation | </tr>|||||||||||||||||||||||||
throttleFirst | computation | </tr>|||||||||||||||||||||||||
throttleLast | computation | </tr>|||||||||||||||||||||||||
throttleWithTimeout | computation | </tr>|||||||||||||||||||||||||
timeInterval | immediate | </tr>|||||||||||||||||||||||||
timeout(timeoutSelector) | immediate | </tr>|||||||||||||||||||||||||
timeout(firstTimeoutSelector,?timeoutSelector) | immediate | </tr>|||||||||||||||||||||||||
timeout(timeoutSelector,?other) | immediate | </tr>|||||||||||||||||||||||||
timeout(timeout,?timeUnit) | computation | </tr>|||||||||||||||||||||||||
timeout(firstTimeoutSelector,?timeoutSelector,?other) | immediate | </tr>|||||||||||||||||||||||||
timeout(timeout,?timeUnit,?other) | computation | </tr>|||||||||||||||||||||||||
timer | computation | </tr>|||||||||||||||||||||||||
timestamp | immediate | </tr>|||||||||||||||||||||||||
window(timespan) | computation | </tr>|||||||||||||||||||||||||
window(timespan,?count) | computation | </tr>|||||||||||||||||||||||||
window(timespan,?timeshift) | computation | </tr> </tbody> </table>