[英]How to execute observable in parallel threads
I have Observable called messages which contains several messages.I want to process these messages at the same time.How can i do this using rxJava?? 我有Observable调用消息,其中包含几条消息。我想同时处理这些消息。我怎样才能使用rxJava?
messages.(code to execute observable items parallel).subscribe(msg->process(msg)) 消息。(并行执行可观察项的代码).subscribe(msg-> process(msg))
(If the observable contains five different messages then I need to process these five messages in five seperate threads) (如果observable包含五个不同的消息,那么我需要在五个单独的线程中处理这五个消息)
If you want to stay in the Observable
world, you can flatMap
each element with subscribeOn
and computation you want in parallel: 如果你想留在
Observable
世界,你可以使用subscribeOn
平行地flatMap
每个元素并且你想并行计算:
Observable.range(1, 10)
.flatMap(v ->
Observable.fromCallable(() -> compute(v))
.subscribeOn(Schedulers.computation)
)
.subscribe(e -> { }, Throwable::printStackTrace);
Run single thread that "observe" your messages, and dispatch every message that it contains to new message handling task (eg simple Runnable
) that is submitted to some sort of workers thread pool . 运行“观察”您的消息的单个线程,并将其包含的每条消息分发给提交给某种工作线程池的新消息处理任务(例如,简单的
Runnable
)。
Here you will find simple how-to: https://docs.oracle.com/javase/tutorial/essential/concurrency/executors.html 在这里,您将找到简单的操作方法: https : //docs.oracle.com/javase/tutorial/essential/concurrency/executors.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.