后台任务如何在调度程序上执行

后台任务如何在调度程序上执行

本文介绍了在 0.18 中,后台任务如何在调度程序上执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感觉我在这里遗漏了一些东西,但我曾经做过的地方:

Feels like I'm missing something here, but where I used to do:

    Schedulers.io().schedule(new Action1<Scheduler.Inner>() {
      @Override
      public void call(Scheduler.Inner inner) {
        doWhatever();
      }
    });

我似乎无法再简单地使用调度程序来运行后台任务,而无需管理取消订阅 (https://github.com/Netflix/RxJava/wiki/Schedulerhttps://github.com/Netflix/RxJava/blob/master/rxjava-core/src/main/java/rx/Scheduler.java).

I don't seem to be able to simply use a scheduler to run a background task anymore, without managing unsubscribes (https://github.com/Netflix/RxJava/wiki/Scheduler and https://github.com/Netflix/RxJava/blob/master/rxjava-core/src/main/java/rx/Scheduler.java).

0.18 是否有一种模式可以让我轻松地运行 doWhatever,而无需跟踪工作人员、取消订阅等?

Is there a pattern for 0.18 that allows me to run doWhatever easily, without keeping track of workers, unsubscribing, etc?

似乎你可以这样做:

final Worker worker = Schedulers.io().createWorker();
worker.schedule(new Action0() {
  @Override
  public void call() {
    doWhatever();
    worker.unsubscribe();
  }
});

但这似乎需要做更多的工作(尤其是对于 android.mainThread 调度程序).

but this seems like a lot more work, (especially for the android.mainThread scheduler).

我在这里错过了什么?

推荐答案

在 Observable 中使用调度器

如果您使用的是 RxJava,我认为您应该让 Observables 处理调度程序.在我的代码中,我不认为我必须创建自己的工人并对其进行管理.

Using Scheduler with Observable

If you are using RxJava, I think you should let Observables deal with the Schedulers. In my code I don't think I ever had to create my own worker and mange it.

将 Observable 与 Scheduler 结合使用并在两种线程类型之间切换的示例.

Example of using Observable with Scheduler and switching between two thread types.

public void doSomething() {
    Observable
            .create(new Observable.OnSubscribe<Boolean>() {
                @Override
                public void call(Subscriber<? super Void> subscriber) {
                    int sleepTime = (int) (Math.random() * 10000);

                    System.out.println("Running on: " + Thread.currentThread().getId() + " sleep: " + sleepTime);

                    try {
                        Thread.sleep(sleepTime);
                    } catch (InterruptedException e) {
                        System.out.println("Error!!! " + Thread.currentThread().getId());
                        subscriber.onError(e);
                        return;
                    }

                    System.out.println("Done!!! " + Thread.currentThread().getId());
                    subscriber.onNext(true);
                    subscriber.onCompleted();
                }
            })
            // this will schedule your work in a background io thread. In this example the "call" method above.
            .subscribeOn(Schedulers.io())
            // this will schedule your next/complete/error on the androids main thread.
            .observeOn(AndroidSchedulers.mainThread())
            // kick off the actual work.
            .subscribe(new Subscriber<Boolean>() {
                @Override
                public void onCompleted() {
                    // runs in main thread.
                }

                @Override
                public void onError(Throwable e) {
                    // runs in main thread.
                }

                @Override
                public void onNext(Boolean result) {
                    // runs in main thread.
                }
            });
}

直接使用调度程序

但是,我知道可能存在需要您直接使用调度程序的情况.所以,如果你想直接使用Scheduler.我认为以下内容符合您的要求.


Using Scheduler Directly

However, I do understand that there might be a case requiring you to use the scheduler directly. So, if you want to use the Scheduler directly. I think the following fits what you are looking for.

使用 runAction 创建调度程序实用程序.

Create a scheduler utility with a runAction.

public static void runAction(Action0 action, Scheduler scheduler) {
    Scheduler.Worker worker = scheduler.createWorker();
    worker.schedule(new Action0() {
        @Override
        public void call() {
            action.call();
            worker.unsubscribe();
        }
    });
}

然后要使用它,传递一个要执行的操作和要使用的调度程序.

Then to use it, pass an action to execute and the scheduler to use.

SchedulerUtil.runAction(new Action0() {
    @Override
    public void call() {
        int sleepTime = (int) (Math.random() * 10000);

        System.out.println("Running on: " + Thread.currentThread().getId() + " sleep: " + sleepTime);
        try {
            Thread.sleep(sleepTime);
        } catch (InterruptedException ignored) {
        }

        System.out.println("Done!!! " + Thread.currentThread().getId());
    }
}, Schedulers.io());

这篇关于在 0.18 中,后台任务如何在调度程序上执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 05:50