Andrew
Andrew

Reputation: 1109

How can I perform flatMap using multiple threads in Reactor?

I have tried running a flatMap on a Flux range followed by subscribeOn and it seems all operations run on the same thread. Is this normal?

Flux.range(0, 1000000).log().flatMap{ it + 1 }.subscribeOn(Schedulers.parallel()).subscribe()

Upvotes: 4

Views: 2031

Answers (2)

Christian Meyer
Christian Meyer

Reputation: 625

Use .subscribeOn(Schedulers.X) to manage subscription

Use .publishOn(Schedulers.X) to manage publishing

Use .parallel(N).runOn(Schedulers.X) when using ParallelFlux.

Upvotes: 0

Rene
Rene

Reputation: 6148

You can create a ParallelFlux as follows:

Flux.range(0, 100000).parallel(2).runOn(Schedulers.parallel()).log().map{ it + 1 }.subscribe()
                      ^^^^^^^^^^^  ^^^^^^use runOn ^^^^^^^^^^^

Upvotes: 4

Related Questions