简体   繁体   English

如何在并行线程中执行observable

[英]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.

相关问题 如何使用多个线程并行运行来执行非常繁重的任务 - How to use several threads to run in parallel to execute a very heavy task java:为什么两个线程不能并行执行 - java: Why does not both threads execute in parallel CompletableFuture 并行执行几个线程,串行执行几个线程 - Execute a few threads in parallel and a few in serial by CompletableFuture 如何观察多个可观察的线程 - How to observe multiple observable threads 如何在多个线程中执行Spring集成流以并行消耗更多Amazon SQS队列消息? - How execute Spring integration flow in multiple threads to consume more Amazon SQS queue messages in parallel? 如何在Android中并行运行线程 - How to run threads in parallel in Android 使用Java线程并行/串行执行一组功能 - Execute group of functions parallel/serially using Java threads 当我执行多个优于Runtime.getRuntime()。availableProcessors()的并行线程时,Java程序的运行速度如何? - How come my Java program goes faster when I execute a number of parallel threads superior to Runtime.getRuntime().availableProcessors())? 如何在黄瓜中执行并行测试 - how to execute parallel testing in cucumber 如何并行执行Hystrix命令? - How to execute Hystrix commands in parallel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM