How to use RxJava Interval Operator

后端 未结 3 876
我在风中等你
我在风中等你 2021-01-17 11:33

I\'m learning about RxJava operator, and I found these code below did not print anything:

public static void main(String[] args) {

    Observable
    .inter         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-01-17 11:35

    As they tell you already interval works asynchronous, so you have to wait for all the events to finish.

    You can get the Subscription once you subscribe and then use TestSubcriber which is part of the reactiveX platform, and which will give you the feature to wait for all events terminates.

           @Test
    public void testObservableInterval() throws InterruptedException {
        Subscription subscription = Observable.interval(1, TimeUnit.SECONDS)
                  .map(time-> "item emitted")
                  .subscribe(System.out::print,
                             item -> System.out.print("final:" + item));
        new TestSubscriber((Observer) subscription)
                .awaitTerminalEvent(100, TimeUnit.MILLISECONDS);
    }
    

    I have in my github more examples if you need https://github.com/politrons/reactive/blob/master/src/test/java/rx/observables/scheduler/ObservableAsynchronous.java

提交回复
热议问题