Multithreading – parallel execution of computationally expensive mappings

I'm new to the reactivex Library (I use its Scala variant, RX Scala)

I have an observable that sends values at a high rate I want to apply a function to all observable maps The functions I use in maps are computationally expensive

Is there a way to make the thread pool compute the map phase in parallel?

Solution

Yes, there are ways to do that

I'll buffer the stream into blocks and use schedulers Computation() (using executors based on a thread pool with a size equal to the number of available processors) distributes the load to CPUs:

int chunkSize = 1000;
source
  .buffer(chunkSize)
  .flatMap(
    list -> 
      Observable
        .from(list)
        .map(expensive)
        .subscribeOn(Schedulers.computation()))
 ...

If the mapping operation is expensive enough, you may perform equally well without buffers:

source
  .flatMap(
    x -> 
      Observable
        .just(x)
        .map(expensive)
        .subscribeOn(Schedulers.computation()))
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>