Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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??

messages.(code to execute observable items parallel).subscribe(msg->process(msg))

(If the observable contains five different messages then I need to process these five messages in five seperate threads)

like image 636
Hussey123 Avatar asked Feb 20 '26 13:02

Hussey123


1 Answers

If you want to stay in the Observable world, you can flatMap each element with subscribeOn and computation you want in parallel:

Observable.range(1, 10)
.flatMap(v -> 
    Observable.fromCallable(() -> compute(v))
    .subscribeOn(Schedulers.computation)
)
.subscribe(e -> { }, Throwable::printStackTrace);
like image 180
akarnokd Avatar answered Feb 23 '26 02:02

akarnokd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!