RXJava 调度程序/线程如何为不同的操作员工作? [英] How RXJava Scheduler/Threading works for different operator?
本文介绍了RXJava 调度程序/线程如何为不同的操作员工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能帮我解释一下哪个调度程序在代码下面运行?
Can anyone please help me to explain which scheduler are running below code?
Completable.complete()
.subscribeOn(http://Schedulers.io ())
.observeOn(AndroidSchedulers.mainThread())
.delay(5000, TimeUnit.MILLISECONDS)
.doOnComplete(() -> liveDataState.postValue(""))
.subscribe()
我的问题是 delay()、doOnComplete() 和 subscribe() 哪些调度程序正在使用 io
或 mainThread
?
My question is which schedulers are delay(), doOnComplete() and subscribe() are using io
or mainThread
?
推荐答案
在过去两天深入研究 RxJava 线程后,发现了处理 RxJava 线程/调度的经验法则:
After digging into RxJava threading last two days found the rule of thumbs for handling RxJava Threading/Scheduling:
observeOn
仅适用于下游运算符subscribeOn
适用于下游和上游运营商- 连续/多个
subscribeOn
不改变线程 - 结果
observeOn
确实改变了下游操作者的线程 - 与
subscribeOn()
不同,我们可以多次使用observeOn()
无缝线程切换 - 像
delay()
、interval()
这样的操作符有默认调度器,也可以改变下游调度器
observeOn
works only downstream operatorsubscribeOn
works for both downstream and upstream operator- Consecutive/Multiple
subscribeOn
do not change the thread - Consequent
observeOn
do change the thread for downstream oerator - Unlike with
subscribeOn()
, we can useobserveOn()
multiple times for seamless thread switching - Operator like
delay()
,interval()
have default scheduler and can change the downstream scheduler as well
所以,就我而言:
Completable.complete() // IO scheduler based on subscribeOn scheduler
.subscribeOn(http://Schedulers.io ())
.observeOn(AndroidSchedulers.mainThread())
.delay(5000, TimeUnit.MILLISECONDS) // Default Computation scheduler
.doOnComplete(() -> liveDataState.postValue("")) // Computation scheduler by delay scheduler
.subscribe() // Computation scheduler by delay as well
此外,您可以查看大理石图以获得更多理解:
Also, you can look into the marble diagram for more understanding:
这篇关于RXJava 调度程序/线程如何为不同的操作员工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文