我在下面尝试过。
public static void main(String[] args) {
Observable<String> zipped = Observable.zip(
Observable.interval(500L, TimeUnit.MILLISECONDS),
Observable.just("A", "B", "C"), (i, v) -> i + v);
zipped.observeOn(
Schedulers.from(Executors.newSingleThreadExecutor()))
.subscribe(System.out::println);
}
自从我使用以来interval
,zipped
observable在不同的线程上运行。我希望该System.out::println
部分在主线程上运行。
我猜想我必须使用observeOn
,但是我不知道必须使用哪个Scheduler返回主线程。
是否可以?
observeOn
除非您使用的是Android ,否则您将无法回到主线程。对于桌面使用,您可以用来toBlocking().subscribe()
在订阅线程上接收值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句