Reactor Flux how to publish in parallel

Viewed 258

Given such example

 Flux.range(0, 10)
                .publishOn(Schedulers.parallel())
                .subscribeOn(Schedulers.parallel())
                .doOnNext(i -> System.out.println(i + " " + Thread.currentThread().getName()))
                .subscribe();

I thought it would publish all items in parallel and in different threads. But it actually processed in single thread sequentially.

0 parallel-2
1 parallel-2
2 parallel-2
3 parallel-2
4 parallel-2
5 parallel-2
6 parallel-2
7 parallel-2
8 parallel-2
9 parallel-2

I know we can use .parallel().runOn(Schedulers.parallel()), but I want to know how to achieve this with publishOn and subscribeOn. Otherwise would subscribeOn and publishOn always execute on single thread? then what is the purpose of Schedulers.parallel()?

0 Answers
Related