Android 在主线程上运行onNext中接收的可运行项的可观察项,项之间有延迟
我希望在主线程上的onNext中接收到运行Android 在主线程上运行onNext中接收的可运行项的可观察项,项之间有延迟,android,multithreading,rx-java,Android,Multithreading,Rx Java,我希望在主线程上的onNext中接收到运行Runnables的Observable,并且项目之间有延迟(在我的代码片段中是2秒) 到目前为止,我已经: Subscription xxx = Observable .create(new Observable.OnSubscribe<Runnable>() { @Override public void call(Subscriber<? super Runnable> subscri
Runnable
s的Observable,并且项目之间有延迟(在我的代码片段中是2秒)
到目前为止,我已经:
Subscription xxx = Observable
.create(new Observable.OnSubscribe<Runnable>() {
@Override
public void call(Subscriber<? super Runnable> subscriber) {
gate = subscriber;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.delay(2, TimeUnit.SECONDS)
.subscribe(new Action1<Runnable>() {
@Override
public void call(Runnable runnable) {
Log.d(TAG, "call " + Thread.currentThread().toString());
runnable.run();
}
});
但是。。。它没有像我想要的那样工作。我想要的是:.observeOn(AndroidSchedulers.mainThread())
假设在主线程上运行可运行程序;但它是在“RxComputationScheduler-2”上运行的
我得到如下输出:
gate requested on thread Thread[Binder_2,5,main]
gate call Thread[RxComputationScheduler-2,5,main]
gate make on thread Thread[RxComputationScheduler-2,5,main]
更改:
.observeOn(AndroidSchedulers.mainThread())
.delay(2, TimeUnit.SECONDS)
致:
谢谢它的工作原理,你能简单解释一下为什么它能解决我的问题吗?我想这是因为“默认情况下,这个延迟变量在计算调度器上运行”。或者更好的
delay(2L,TimeUnit.SECONDS,AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.delay(2, TimeUnit.SECONDS)
.delay(2, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())