Search code examples
javaexecutorserviceproject-reactorflux

Project Reactor create multiple scheduler from the same Executor


In my application I have a single java Executor, can it create issues if I create multiple Scheduler from the same instance of Executor, having something like:

public class MyServiceController {
    @Autowired
    private Executor mainExecutor;

    public Object something() {
         return Flux.from(somethingElse())
                    .publishOn(Schedulers.fromExecutor(mainExecutor))
                    .toFuture()
    }

}

(Or having multiple classes implementing this pattern, all of them having the same instance of mainExecutor)


Solution

  • it should be fine in both cases, the same Executor will back all Scheduler.Worker spawned by each Scheduler you create from it, and the same is true if it is an ExecutorService (albeit with a different Scheduler implementation for the wrapper).

    For clarity, I'd still consider making the Scheduler a singleton next to the Executor.