What are the situations which cause Flux::flatMap
to listen to multiple sources (0...infinity) concurrently?
I found out, while experimenting, that when the upstream send signals to flatMap
in thread thread-upstream-1
and there are N
inner streams which flatMap will listen to and each of them send signals in different thread: thread-inner-stream-i
for 1<=i<=N
, than for every 1<=i<=N
if thread-upstream-1 != thread-inner-stream-i
, flatMap
will listen concurrently to all the inner streams.
I think that it's not exactly true and I missed some other scenarios.
flatMap
doesn't do any parallel work, as in: it doesn't change threads. The simplest example isThis prints:
As you can see, only produces in
main
. If you add apublishOn
after the initial range,flatMap
produces everything in the same single thread publishOn will switch to.What
flatMap
does however is subscribe to multiple innerPublisher
, up to theconcurrency
parameter with a default ofQueues.SMALL_BUFFER_SIZE
(256).That means that if you set it to
3
,flatMap
will map 3 source elements to their innerPublisher
and subscribe to these publishers, but will wait for at least one to complete before it starts mapping more source elements.If the inner
Publisher
usepublishOn
orsubscribeOn
, thenflatMap
will naturally let their events occur in the then-defined threads:Which prints: