delay and interval operators do not work properly

前端 未结 1 924
旧时难觅i
旧时难觅i 2021-01-27 12:10

As shown below, I am creating Observables. I would like to wait specific amount of time in seconds as shown in the code. therefore I used either delay or interval o

1条回答
  •  北恋
    北恋 (楼主)
    2021-01-27 12:44

    You have to wait in the main method. Put Thread.sleep(10000) at the very end of the main() method so the Observable has chance to run. RxJava threads are daemon threads that stop when the application thread falls out of the main() method.

    public static void main(String[] args) {
    
        Observable.just("Hello World!", "Keep printing values!")
        .zipWith(Observable.interval(0, 5, TimeUnit.SECONDS), (a, b) -> a)
        .subscribe(v -> 
            System.out.println(Thread.currentThread() + ": " + v)
        );
    
        Thread.sleep(10000);  // <-----------------------------------
    
    }
    

    0 讨论(0)
提交回复
热议问题