RxJava interval操作符

Interval操作符:
创建一个按照固定时间发射整数序列的Observable
注意:在写测试代码的时候,可能会碰到这种情况,按照其他一些教程上的吧代码敲上去,结果运行不出结果来:如下所示:

    @Test
    public void testOperatorInterval() {
        Observable.interval(1, TimeUnit.SECONDS).subscribe(new Consumer<Long>() {

            @Override
            public void accept(Long value) throws Exception {
                System.out.println("emitter value is " + value);
            }
        });
    }

    不执行每隔一秒输出一个数字的结果的整个解释是:
    When you use the default scheduler (Schedulers.computation()) the observable emits on another thread.
    当您使用默认调度程序(Schedulers.computation())时,observable将在另一个线程上发出。 
    
    If your program exits just after the subscribe then the observable is not given a chance to run. 
    如果你的程序在订阅之后退出,则observable没有机会运行。
    
    Put in a long sleep just after the subscribe() call and you will see it working.
    在subscribe()调用之后进行长时间的睡眠,您将看到它正常工作。
    
    源码如下:

    @SchedulerSupport(SchedulerSupport.COMPUTATION)
    public static Observable<Long> interval(long period, TimeUnit unit) {
        return interval(period, period, unit, Schedulers.computation());
    }

    原来在 interval()内部调用的是 interval(period, period, unit, Schedulers.computation()),这其中使用的默认调度器是 Schedulers.computation()
    而Schedulers.computation() 然后就是上面的解释,具体可参考 https://github.com/ReactiveX/RxJava/issues/4132 这里的原版解释,
    所以此处应该使用Schedulers.trampoline()

    将代码修改为如下所示即可达到效果:
    普通写法:

    @Test
    public void testOperatorInterval() {
        Observable.interval(1,TimeUnit.SECONDS,Schedulers.trampoline()).subscribe(new Consumer<Long>() {

            @Override
            public void accept(Long value) throws Exception {
                System.out.println("emitter value is " + value);
            }
        });
    }

    lambda表达式写法:
    @Test
    public void testOperatorInterval() {
        Observable.interval(1,TimeUnit.SECONDS,Schedulers.trampoline()).subscribe(emitter -> System.out.println("emitter value is:" + emitter));
    }

    
    上面的是无限制的发送数据的,但是我们肯定不是想要它一直无限制的发啊,所以需要使用的是定制化的发送。所以需要使用 Observable.intervalRange(...)
    而且Observable.intervalRange(...)有多个重载方法,可以按需选择。
    示例如下:
      普通写法:
    @Test
    public void testOperatorInterval() {

        //表示从20(包括)开始,发射20条数据,发射前延迟300ms,然后每隔200ms发射一条数据,使用的是Schedulers.trampoline()调度器
        Observable.intervalRange(20, 20, 300, 200, TimeUnit.MILLISECONDS, Schedulers.trampoline())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long value) throws Exception {
                        System.out.println("emitter value is:" + value);
                    }
                });
    }

    lambda表达式写法:
    @Test
    public void testOperatorInterval() {
        
        Observable.intervalRange(20, 20, 300, 200, TimeUnit.MILLISECONDS, Schedulers.trampoline())
        .subscribe(emitter -> System.out.println("emitter value is:" + emitter));
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值