java RxJava 单后台线程调度器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29704451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RxJava single background thread scheduler
提问by casolorz
I'm fairly new to RxJava so this is probably a dumb question. I am going to describe my scenario.
我对 RxJava 还很陌生,所以这可能是一个愚蠢的问题。我将描述我的场景。
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. 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 线程上运行了一些代码,它们会更新一些图像,但这些图像不是很重要,它们在生成它们时会消耗一些资源,所以我想在单个线程上生成它们(当然不是 UI 线程)和一一生成。我猜蹦床调度程序是我想要的,但我的问题是,如果我使用它,那么它会在 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?" 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 线程上?” 总是有真的。那么我如何使该代码和所有后续尝试在单个线程(而不是 ui 线程)上按顺序运行,而无需编写一堆代码来排队该工作?
Edit:
编辑:
I believe that this can now be accomplished using Schedulers.single()
or if you want your own you can use new SingleScheduler()
. I'm still testing but I think it does what I wanted back when I posted this.
我相信现在Schedulers.single()
可以使用new SingleScheduler()
. 我仍在测试,但我认为当我发布此内容时,它可以满足我的要求。
采纳答案by corsair992
You can create a single reusable thread to create a Scheduler
for the Observable
in one of the following ways:
您可以通过以下方式之一创建单个可重用线程来创建Scheduler
for Observable
:
- 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 theSchedulers.from()
method. - RxAndroidprovides a custom
Scheduler
implementation that uses aHandler
to schedule the actions, and thus can be used with anyThread
that has aLooper
running by passing it'sHandler
to theAndroidSchedulers.handlerThread()
factory method.
- 创建一个
ThreadPoolExecuter
池大小为 1 的(这Executors.newSingleThreadExecutor()
是一个方便的静态工厂方法),然后使用它通过该Schedulers.from()
方法生成调度程序。 - RxAndroid提供了一个自定义的
Scheduler
,它使用一个执行Handler
安排的动作,因此可以与任何使用Thread
具有Looper
通过将它的运行Handler
的AndroidSchedulers.handlerThread()
工厂方法。
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.
请注意,Scheduler
如果您在这些任务结束时与 UI 交互,则需要在主线程上进行观察。
回答by Albert Vila Calvo
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 documentationfor more details.
有关更多详细信息,请参阅文档。
I don't see it available in RxJava 1 Schedulers documentation.
我在 RxJava 1 Schedulers 文档中没有看到它。
回答by Trieu Tran
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. That why your log shows your are running on the main thread
并且subscribeOn
将适用于上游和下游。这就是为什么您的日志显示您正在运行main thread
To fix this, you can use Schedulers.single()
in the subscribeOn
, and then observeOn
the main thread.
要解决此问题,您可以Schedulers.single()
在 中使用subscribeOn
,然后observeOn
在主线程中使用。