ScheduledExecutorService multiple threads in parallel ScheduledExecutorService multiple threads in parallel multithreading multithreading

ScheduledExecutorService multiple threads in parallel


I've solved the problem by launching a nested anonymous runnable in each scheduled execution:

final ScheduledExecutorService service = Executors.newScheduledThreadPool(POOL_SIZE);final Runnable command = new SlowRunnable();service.scheduleAtFixedRate(    new Runnable() {      @Override      public void run() {        service.execute(command);      }    }, 0, 1, TimeUnit.SECONDS);

With this example there will be 1 thread executing at every interval a fast instruction, so it will be surely be finished when the next interval is expired. The remaining POOL_SIZE-1 threads will be executing the SlowRunnable's run() in parallel, which may take longer time than the duration of the single interval.

Please note that while I like this solution as it minimize the code and reuse the same ScheduledExecutorService, it must be sized correctly and may not be usable in every context: if the SlowRunnable is so slow that up to POOL_SIZE jobs get executed together, there will be no threads to run the the scheduled task in time.

Also, if you set the interval at 1 TimeUnit.NANOSECONDS it will probably became too slow also the execution of the main runnable.


One of the scheduleAtFixedRate methods is what you're looking for. It starts a task in a thread from the pool at the given interval, even if previous tasks haven't finished. If you're running out of threads to do the processing, adjust the pool size constraints as detailed in the ThreadPoolExecutor docs.