Search code examples
javaasynchronousfluxreactor

do operators of Flux in Project Reactor execute asynchronously?


For example, flux.map(i->{Thread.sleep(1000); return i*i;}).flatMap(i->Monn.just(i)). Does the map operator execute asynchronously in nature or we have to change the thread so it can execute asynchronously (like we switch to flux.map(i->{Thread.sleep(1000); return i*i;}).subscribeOn(Schedulers.elastic()).flatMap(i->Monn.just(i)))?

And this example: flux.map(i->i*i).flatMap(i->Monn.just(i)). Does the map opeator execute asynchronously in nature or we have to change the thread so it can execute asynchronously like we do in the last paragraph?


Solution

  • Obtaining a Flux or a Mono does not necessarily mean that it runs in a dedicated Thread. Instead, most operators continue working in the Thread on which the previous operator executed. Unless specified, the topmost operator (the source) itself runs on the Thread in which the subscribe() call was made. The above is what I want to know.