简体   繁体   English

RxJava单个后台线程调度程序

[英]RxJava single background thread scheduler

I'm fairly new to RxJava so this is probably a dumb question. 我对RxJava很新,所以这可能是一个愚蠢的问题。 I am going to describe my scenario. 我将描述我的情景。

I have some code running on the UI thread which will update some images but those images are not very important and they consume a bit of resources while generating them so I want to generate them on a single thread (not the UI thread of course) and generate them one by one. 我在UI线程上运行了一些代码,它们将更新一些图像,但这些图像不是很重要,它们在生成它们时消耗了一些资源,所以我想在单个线程上生成它们(当然不是UI线程)和逐个生成它们。 I'm guessing the trampoline scheduler is what I want but my problem is that if I use it then it does the work on the UI thread and I want it to do it on another thread. 我猜蹦床调度程序是我想要的,但我的问题是,如果我使用它然后它在UI线程上工作,我希望它在另一个线程上做。

Obviously I can write my own thread in which I can queue items and then it processes those one by one but I thought maybe RxJava would have a simple solution for me? 显然,我可以编写自己的线程,我可以在其中对项目进行排队,然后逐个处理这些,但我想RxJava可能会有一个简单的解决方案吗?

My current code looks like this: 我当前的代码如下所示:

Observable<Bitmap> getImage = Observable.create(new Observable.OnSubscribe<Bitmap>() {
    @Override public void call(Subscriber<? super Bitmap> subscriber) {
        Log.w(TAG,"On ui thread? "+ UIUtils.isRunningOnUIThread());
        subscriber.onNext(doComplexTaskToGetImage());
        subscriber.onCompleted();
    }
});

getImage.subscribeOn(Schedulers.trampoline()).subscribe(new Action1<Bitmap>() {
    @Override public void call(Bitmap bitmap) {
        codeToSetTheBitmap(bitmap);
    }
});

My log that says "On ui thread?" 我的日志上写着“On ui thread?” always has true. 永远都是如此。 So how do I make that code and all subsequent attempts to do the same thing run on a single thread (not the ui thread) in order without writing a bunch of code to queue that work? 那么我如何使代码和所有后续尝试做同样的事情按顺序在单个线程(而不是ui线程)上运行而不编写一堆代码来排队工作?

Edit: 编辑:

I believe that this can now be accomplished using Schedulers.single() or if you want your own you can use new SingleScheduler() . 我相信现在可以使用Schedulers.single()完成此操作,或者如果您想要自己的,可以使用new SingleScheduler() I'm still testing but I think it does what I wanted back when I posted this. 我还在测试,但是当我发布这个帖子时,我认为它确实符合我的要求。

You can create a single reusable thread to create a Scheduler for the Observable in one of the following ways: 您可以使用以下方法之一创建一个可重用的线程来为Observable创建一个Scheduler

  • Create a ThreadPoolExecuter with a pool size of 1 ( Executors.newSingleThreadExecutor() is a convenient static factory method for doing that), then use it to generate the schedulers via the Schedulers.from() method. 创建一个池大小为1的ThreadPoolExecuterExecutors.newSingleThreadExecutor()是一个方便的静态工厂方法),然后使用它通过Schedulers.from()方法生成调度Schedulers.from()
  • RxAndroid provides a custom Scheduler implementation that uses a Handler to schedule the actions, and thus can be used with any Thread that has a Looper running by passing it's Handler to the AndroidSchedulers.handlerThread() factory method. RxAndroid提供了一个自定义Scheduler实现,它使用Handler来调度操作,因此可以通过将其Handler传递给AndroidSchedulers.handlerThread()工厂方法,与任何运行Looper Thread一起使用。

Note that you will need to observe on a main thread Scheduler if you're interacting with the UI at the conclusion of these tasks. 请注意,如果您在完成这些任务时与UI交互,则需要在主线程Scheduler上进行观察。

In RxJava 2 you can use Schedulers.single() which: 在RxJava 2中,您可以使用Schedulers.single() ,其中:

Returns a default, shared, single-thread-backed Scheduler instance for work requiring strongly-sequential execution on the same background thread. 返回默认的,共享的,单线程支持的Scheduler实例,用于需要在同一后台线程上执行强序执行的工作。

Please see the documentation for more details. 有关更多详细信息,请参阅文档

I don't see it available in RxJava 1 Schedulers documentation . 我在RxJava 1 Schedulers文档中看不到它。

You are using trampoline scheduler, so it means your source observable will be run on the current thread (here is main thread). 您正在使用trampoline调度程序,因此这意味着您的源observable将在当前线程上运行(这里是主线程)。

And the subscribeOn will work for both upstream and downstream. subscribeOn将适用于上游和下游。 That why your log shows your are running on the main thread 这就是您的日志显示您正在main thread上运行的原因

To fix this, you can use Schedulers.single() in the subscribeOn , and then observeOn the main thread. 要解决此问题,您可以在subscribeOn使用Schedulers.single() ,然后在主线程中observeOn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM