简体   繁体   English

RxJava。 Observable.delay工作奇怪(最后缺少一些项目)

[英]RxJava. Observable.delay work strange (lacks some items at the end)

I'm trying to understand RxJava. 我正在努力了解RxJava。 My test code is: 我的测试代码是:

import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;

import java.util.concurrent.TimeUnit;

public class Hello {
    public static void main(String[] args) {

            Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                try {
                    Thread.sleep(1000);
                    subscriber.onNext("a");
                    Thread.sleep(1000);
                    subscriber.onNext("b");
                    Thread.sleep(1000);
                    subscriber.onNext("c");
                    Thread.sleep(1000);
                    subscriber.onNext("d");
                    Thread.sleep(1000);
                    subscriber.onNext("e");
                    Thread.sleep(1000);
                    subscriber.onNext("f");
                    Thread.sleep(1000);
                    subscriber.onNext("g");
                    Thread.sleep(1000);
                    subscriber.onNext("h");
                } catch (InterruptedException e) {
                    subscriber.onError(e);
                }
            }
        });

        observable
                .delay(2, TimeUnit.SECONDS)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String string) {
                        System.out.println(string);
                    }
                });
    }
}

Without .delay(2, TimeUnit.SECONDS) i have the output: abcdefgh but with .delay(2, TimeUnit.SECONDS) the output lacks "g" and "h": abcdef 没有.delay(2, TimeUnit.SECONDS)我输出:abcdefgh但是带有.delay(2, TimeUnit.SECONDS)输出缺少“g”和“h”:abcdef

How can that be? 怎么可能? Documentation says that delay just emits the items emitted by the source Observable shifted forward in time by a specified delay 文档说延迟只是发出源Observable发出的项目在时间上向前移动指定的延迟

The delay overload that you are using schedules work on a different thread and results in an implicit race condition.All temporal operators (such as delay , buffer , and window ) need to use a scheduler to schedule the effect for later and this can result in unexpected race conditions if you aren't aware of it and use them carefully. 您正在使用调度的delay重载在不同的线程上工作并导致隐式竞争条件。所有时间运算符(如delaybufferwindow )需要使用调度程序为以后计划效果,这可能导致意外的竞争条件,如果你不知道它并仔细使用它们。 In this case the delay operator schedules the work downstream on a separate thread pool. 在这种情况下,延迟操作员在单独的线程池上调度下游工作。 Here is the order of execution (on the main thread) in your test. 以下是测试中的执行顺序(在主线程上)。

  1. Your Observable is subscribed to and waits 1000 millis before onNext("a") 您的Observable已订阅并在onNext("a")之前等待1000 onNext("a")
  2. Next it's received by the delay. 接下来是延迟收到的。 This schedules the downstream onNext for 2 seconds later. 这将调度下游onNext 2秒钟。
  3. Control flow returns immediately to your observable which waits 1000 millis. 控制流程立即返回到您的观察点,等待1000毫秒。
  4. Observable onNext("b") to delay. 可观察onNext("b")延迟。 Delay schedules the onNext of "b" for 2 seconds later. 延迟将“b”的onNext计划为2秒钟。
  5. .... (repeat) ....(重复)
  6. When your observable calls onNext("h") it schedules the work then immediately returns from subscribe and terminates your test (causing the scheduled work to disappear). 当您的observable调用onNext("h")它会调度工作,然后立即从subscribe返回并终止您的测试(导致计划的工作消失)。

In order to get it to execute asynchronously you you can schedule the delay on the trampoline scheduler implementation. 为了让它以异步方式执行,您可以安排trampoline调度程序实现的延迟。

.delay(2, TimeUnit.SECONDS, Schedulers.trampoline())

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

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