RxJava & RxAndroid简单使用(3)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Song_74110/article/details/70050029

RxJava的异步机制线程的调度Scheduler,RxJava内部有一个Schedulers类,里面有5种Scheduler类型,1.0里面有Schedulers.immediate( ),而在2.0就去掉了,2.0有个新的类型Schedulers.single()。最常用的就两种:Schedulers.io()AndroidSchedulers.mainThread()

Scheduler类型

  • Schedulers.io() I/O 操作 内部实现是是用一个无数量上限的线程池
    /**
     * Creates and returns a {@link Scheduler} intended for IO-bound work.
     * <p>
     * The implementation is backed by an {@link Executor} thread-pool that will grow as needed.
     * <p>
     * This can be used for asynchronously performing blocking IO.
     * <p>
     * Do not perform computational work on this scheduler. Use {@link #computation()} instead.
     * <p>
     * Unhandled errors will be delivered to the scheduler Thread's {@link java.lang.Thread.UncaughtExceptionHandler}.
     *
     * @return a {@link Scheduler} meant for IO-bound work
     */
    public static Scheduler io() {
        return RxJavaPlugins.onIoScheduler(IO);
    }
  • Schedulers.single() 单线程操作 2.0以上才加的,貌似1.0没有
    /**
     * Returns the common, single-thread backed Scheduler instance.
     * <p>
     * Uses:
     * <ul>
     * <li>main event loop</li>
     * <li>support Schedulers.from(Executor) and from(ExecutorService) with delayed scheduling</li>
     * <li>support benchmarks that pipeline data from the main thread to some other thread and
     * avoid core-bashing of computation's round-robin nature</li>
     * </ul>
     * @return a {@link Scheduler} that shares a single backing thread.
     * @since 2.0
     */
    public static Scheduler single() {
        return RxJavaPlugins.onSingleScheduler(SINGLE);
    }
  • Schedulers.immediate( )在当前线程立即开始执行任务 这个是1.0有,2.0没有这个类型了
1.0immediate()方法的源码这里就不贴出来了
  • Schedulers.newThread() 每次都启用新线程,并在新线程执行操作
    /**
     * Creates and returns a {@link Scheduler} that creates a new {@link Thread} for 
     * each unit of work.
     * <p>
     * Unhandled errors will be delivered to the scheduler 
     * Thread's {@link java.lang.Thread.UncaughtExceptionHandler}.
     *
     * @return a {@link Scheduler} that creates new threads
     */
    public static Scheduler newThread() {
        return RxJavaPlugins.onNewThreadScheduler(NEW_THREAD);
    }
  • Schedulers.trampoline() 当其它排队的任务完成后,在当前线程排队开始执行
    /**
     * Creates and returns a {@link Scheduler} that queues work on the current thread 
     * to be executed after the
     * current work completes.
     *
     * @return a {@link Scheduler} that queues work on the current thread
     */
    public static Scheduler trampoline() {
        return TRAMPOLINE;
    }
  • Schedulers.computation() 用于计算任务,如事件循环或和回调处理,不要用于IO操作(IO操作请使用Schedulers.io());默认线程数等于处理器的数量
    /**
     * Creates and returns a {@link Scheduler} intended for computational work.
     * <p>
     * This can be used for event-loops, processing callbacks and other computational work.
     * <p>
     * Do not perform IO-bound work on this scheduler. Use {@link #io()} instead.
     * <p>
     * Unhandled errors will be delivered to the scheduler Thread's
     * {@link java.lang.Thread.UncaughtExceptionHandler}.
     *
     * @return a {@link Scheduler} meant for computation-bound work
     */
    public static Scheduler computation() {
        return RxJavaPlugins.onComputationScheduler(COMPUTATION);
    }
  • Schedulers.from(executor)
    /**
     * Converts an {@link Executor} into a new Scheduler instance.
     *
     * @param executor
     *          the executor to wrap
     * @return the new Scheduler wrapping the Executor
     */
    public static Scheduler from(Executor executor) {
        return new ExecutorScheduler(executor);
    }

Schedulers的简单使用

        Observable.just(1, 2, 3)
                .subscribeOn(Schedulers.io())             //订阅事件发生在io线程
                .observeOn(AndroidSchedulers.mainThread())//在主线程中观察
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Integer value) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

猜你喜欢

转载自blog.csdn.net/Song_74110/article/details/70050029