tata88k / rxjava2-discussion

rxjava2-discussion
16 stars 2 forks source link

delay的疑惑 #2

Open Alex-Cin opened 7 years ago

Alex-Cin commented 7 years ago

Observable.just("Alex").delay(1000, TimeUnit.MILLISECONDS)
                .doOnSubscribe(new LiteConsumer<Disposable>() {
                    @Override
                    public void onNext(Disposable result) {
                        LogTrack.e("只有 这里 会执行");
                    }
                })
                .doOnTerminate(new Action() {
                    @Override
                    public void run() throws Exception {
                        LogTrack.e(" -- ");
                    }
                })
                .doFinally(new Action() {
                    @Override
                    public void run() throws Exception {
                        LogTrack.e("---");
                    }
                })
                .subscribe(new LiteObserver<String>() {
                    @Override
                    public void onNext(String result) {
                        LogTrack.e(result);
                    }
                });
如果 没有delay的话, 一切正常;
如果 有delay的话,一切都不正常了;
tysheng commented 7 years ago

线程控制呢?如果是main函数,这样做是得不到回调的,如果是Android中,始终有个主线程在,可以得到回调

Alex-Cin commented 7 years ago

@tysheng

这里应该这么写吗?

Observable.just("Alex")
                .subscribeOn(Schedulers.trampoline())
                .observeOn(Schedulers.trampoline())
                .delay(1000, TimeUnit.MILLISECONDS)

好像写成切换 也是不好使的, 是在 main 函数里面写的。

tata8k commented 7 years ago

@Alex-Cin 你在主线程里面sleep一下就看到了,也就是在你的main函数里面sleep一下。

Alex-Cin commented 7 years ago

@UsherBaby thread 的run方法 包裹 Observable.just.subscribe 所有回调都是正常的 其中sleep 要写在 Observable操作的后面, 求解。。。

tysheng commented 7 years ago

你在主线程中对一个子线程说,过一秒钟我再回来拿个东西,子线程就去做了,当说完这句话时,主线程看看还有没有事,没事就关了,sleep为了让主线程别关

Alex-Cin commented 7 years ago

@tysheng 多谢, 明白了

tata8k commented 7 years ago

@Alex-Cin 因为子线程没执行完,你主线程已经关闭了,所以你看不到。还有,请不关闭Issue。