android Observable.interval定时执行快速上手和分析

Observable.interval可以执行定时任务, Observable.interval是来源于Rxjava2的函数,我引用的是:

 compile 'io.reactivex.rxjava2:rxjava:2.2.6'

使用如下:

import java.util.concurrent.TimeUnit;
 
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
 
private Disposable disposable;
 
/**
 * 开始定时执行
 */
private void startTimer() {
    stopTimer();
    // 每隔200毫秒执行一次逻辑代码
    disposable = Observable.interval(200, TimeUnit.MILLISECONDS)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Consumer() {
                @Override
                public void accept(@io.reactivex.annotations.NonNull Long aLong) throws Exception {
                    // 逻辑代码
                }
            }, new Consumer() {
                @Override
                public void accept(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
                    throwable.printStackTrace();
                }
            });
}
 
/**
 * 停止定时执行
 */
protected void stopTimer() {
    if (null != disposable) {
        disposable.dispose();
        disposable = null;
    }
}

分析:
1、上面使用中的Observable.interval(200, TimeUnit.MILLISECONDS)的参数的第二个,可以选择不同的时间类型,可以选秒,分钟,小时等,见源码:

 SECONDS {
        public long toNanos(long d)   { return x(d, C3/C0, MAX/(C3/C0)); }
        public long toMicros(long d)  { return x(d, C3/C1, MAX/(C3/C1)); }
        public long toMillis(long d)  { return x(d, C3/C2, MAX/(C3/C2)); }
        public long toSeconds(long d) { return d; }
        public long toMinutes(long d) { return d/(C4/C3); }
        public long toHours(long d)   { return d/(C5/C3); }
        public long toDays(long d)    { return d/(C6/C3); }
        public long convert(long d, TimeUnit u) { return u.toSeconds(d); }
        int excessNanos(long d, long m) { return 0; }
    },

2、interval重写了四个函数,如下:

/**
 * @param period 每次执行的间隔的时间
 * @param unit 时间单位
 * @return Observable对象
 */
 
  @CheckReturnValue
    @SchedulerSupport(SchedulerSupport.COMPUTATION)
    public static Observable interval(long period, TimeUnit unit) {
        return interval(period, period, unit, Schedulers.computation());
    }
/**
 * @param initialDelay 第一次执行的延迟时间
 * @param period 每次执行的间隔的时间
 * @param unit 时间单位
 * @return Observable对象
 */
  @CheckReturnValue
    @SchedulerSupport(SchedulerSupport.COMPUTATION)
    public static Observable interval(long initialDelay, long period, TimeUnit unit) {
        return interval(initialDelay, period, unit, Schedulers.computation());
    }
   /**
 * @param period 每次执行的间隔的时间
 * @param unit 时间单位
 * @param scheduler 线程调度器
 * @return Observable对象
 */

  @CheckReturnValue
    @SchedulerSupport(SchedulerSupport.CUSTOM)
    public static Observable interval(long period, TimeUnit unit, Scheduler scheduler) {
        return interval(period, period, unit, scheduler);
    }
/**
 * @param initialDelay 第一次执行的延迟时间
 * @param period 每次执行的间隔的时间
 * @param unit 时间单位
 * @param scheduler 线程调度器
 * @return Observable对象
 */
   @CheckReturnValue
    @NonNull
    @SchedulerSupport(SchedulerSupport.CUSTOM)
    public static Observable interval(long initialDelay, long period, TimeUnit unit, Scheduler scheduler) {
        ObjectHelper.requireNonNull(unit, "unit is null");
        ObjectHelper.requireNonNull(scheduler, "scheduler is null");

        return RxJavaPlugins.onAssembly(new ObservableInterval(Math.max(0L, initialDelay), Math.max(0L, period), unit, scheduler));
    }

3、Scheduler 线程调度器是干啥的?

见文章:https://blog.csdn.net/suyimin2010/article/details/92532527

你可能感兴趣的:(语音直播间专栏,android)