美文网首页
依赖于rxjava的定时器

依赖于rxjava的定时器

作者: sjj_dot | 来源:发表于2018-11-14 16:35 被阅读0次
    import android.support.annotation.NonNull;
    
    import java.util.concurrent.TimeUnit;
    
    import io.reactivex.Scheduler;
    import io.reactivex.disposables.Disposable;
    import io.reactivex.functions.Consumer;
    import io.reactivex.schedulers.Schedulers;
    import sjj.alog.Log;
    
    /**
     *
     * 通用定时任务。rxjava
     * @author shenjianjun
     *
     */
    public final class ThreadPool {
        private ThreadPool() {
        }
    
        public static Disposable submit(@NonNull Scheduler scheduler, @NonNull final Runnable runnable, @NonNull final Consumer<Throwable> consumer) {
            return scheduler.scheduleDirect(() -> {
                try {
                    runnable.run();
                } catch (Exception e) {
                    try {
                        consumer.accept(e);
                    } catch (Exception ignored) {
                    }
                }
            });
        }
    
        public static Disposable submit(@NonNull Runnable runnable, @NonNull Consumer<Throwable> consumer) {
            return submit(Schedulers.computation(), runnable, consumer);
        }
    
        public static Disposable submit(@NonNull Scheduler scheduler, @NonNull final Runnable runnable) {
            return submit(scheduler, runnable, throwable -> Log.e("pool error", throwable));
        }
    
        public static Disposable submit(@NonNull final Runnable runnable) {
            return submit(Schedulers.computation(), runnable);
        }
    
        public static Disposable submit(@NonNull Scheduler scheduler, @NonNull final Runnable run, long delay, @NonNull final Consumer<Throwable> consumer) {
            return scheduler.scheduleDirect(() -> {
                try {
                    run.run();
                } catch (Exception e) {
                    try {
                        consumer.accept(e);
                    } catch (Exception ignored) {
                    }
                }
            }, delay, TimeUnit.MILLISECONDS);
        }
    
        public static Disposable submit(@NonNull Runnable runnable, long delay, @NonNull Consumer<Throwable> consumer) {
            return submit(Schedulers.computation(), runnable, delay, consumer);
        }
    
        public static Disposable submit(@NonNull Scheduler scheduler, @NonNull final Runnable runnable, long delay) {
            return submit(scheduler, runnable, delay, throwable -> Log.e("pool error", throwable));
        }
    
        public static Disposable submit(@NonNull final Runnable runnable, long delay) {
            return submit(Schedulers.computation(), runnable, delay);
        }
    
        public static Disposable submit(@NonNull Scheduler scheduler, @NonNull final Runnable run, final long initialDelay, final long period, @NonNull final Consumer<Throwable> consumer) {
            return scheduler.schedulePeriodicallyDirect(() -> {
                try {
                    run.run();
                } catch (Exception e) {
                    try {
                        consumer.accept(e);
                    } catch (Exception ignored) {
                    }
                }
            }, initialDelay, period, TimeUnit.MILLISECONDS);
        }
    
        public static Disposable submit(@NonNull Runnable runnable, final long initialDelay, final long period, @NonNull Consumer<Throwable> consumer) {
            return submit(Schedulers.computation(), runnable, initialDelay, period, consumer);
        }
    
        public static Disposable submit(@NonNull Scheduler scheduler, @NonNull final Runnable runnable, final long initialDelay, final long period) {
            return submit(scheduler, runnable, initialDelay, period, throwable -> Log.e("pool error", throwable));
        }
    
        public static Disposable submit(@NonNull final Runnable runnable, final long initialDelay, final long period) {
            return submit(Schedulers.computation(), runnable, initialDelay, period);
        }
    }
    

    相关文章

      网友评论

          本文标题:依赖于rxjava的定时器

          本文链接:https://www.haomeiwen.com/subject/bpmjfqtx.html