Search code examples
javamultithreadingscheduledexecutorservice

Thread queuing in ScheduledExecutorService


I have a doubt regarding how ScheduledExecutorService works, let's say I have declared an executor like so:

private static final ScheduledExecutorService SCHEDULED_EXECUTOR = Executors.newScheduledThreadPool(2);

And, this is the task I'll be creating to be run when a day passes:

SCHEDULED_EXECUTOR.schedule(() -> {
    // do stuff
}, 1, TimeUnit.DAYS);

If four petitions are received at the same time, do all the timers run and then, only two threads are run in parallel? Or just two timers run in parallel?


Solution

  • You configured your ExecutorService to go with two threads. That means that (at maximum) two threads will be serving tasks in parallel.

    So when 4 submits come in, they will be "dispatched" to those two threads.

    That is all there is to it.