简体   繁体   English

RxJava just()计算线程

[英]RxJava just() computation thread

Im confused. 我糊涂了。 I have a method which do some longtime job (about 5 second) and returns a String as a result. 我有一个方法,可以做一些长时间的工作(大约5秒钟),并返回一个String结果。 I wrapped that function into an Observable smth like this: 我将该函数包装到一个Observable smth中,如下所示:

private Observable<String> getJobObservable() {
    return Observable.just(doJob());
}

But even with .subscribeOn(Schedulers.computation()) and .observeOn(Schedulers.computation()) my doJob() method calculates on mainThread But, after i have changed Observable.just() to Observable.fromCallable() everything works fine. 但是即使使用.subscribeOn(Schedulers.computation()).observeOn(Schedulers.computation())我的doJob()方法mainThreadmainThread计算,但是,在将Observable.just()更改为Observable.fromCallable()一切正常。 Why Observable.just() doesn't react on thread changes? 为什么Observable.just()对线程更改没有反应?

If you write 如果你写

return Observable.just(doJob());

that is equivalent to 相当于

Object o = doJob();
return Observable.just(o);

Now if you don't have Observable.just(o) at all, where does doJob() execute? 现在,如果您根本没有Observable.just(o) ,那么doJob()在哪里执行? Just because doJob() or o are put into between the parenthesis of just , it doesn't mean somehow the whole execution of doJob gets deferred, unlike when you ensure it via fromCallable . 仅仅因为doJob()o放在了just的括号之间,这并不意味着doJob的整个执行会被推迟,这与您通过fromCallable确保它fromCallable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM