I’m using java.util.concurrent.ExecutorService with fixed thread pool to execute list of tasks. My list of tasks will typically be around 80 – 150 and I’ve limited the number of threads running at any time to 10 as shown below:
ExecutorService threadPoolService = Executors.newFixedThreadPool(10);
for ( Runnable task : myTasks )
{
threadPoolService.submit(task);
}
My use case demands that even the completed task should be re-submitted again to the ExecutorService but it should be executed/taken again only when all the already submitted tasks are serviced/completed. That is basically, the tasks submitted should be executed on a rotation-basis. Hence, there will not be either threadPoolService.shutdown() or threadPoolService.shutdownNow() call in this case.
My question is, how do I implement ExecutorService servicing rotation-basis tasks?
ThreadPoolExecutor provides an extension point for afterExecution where you can put the job back at the end of the queue.
You’ll have to do a little more work of course to instantiate it yourself without the help of
ExecutorService‘s handy factory method, but the constructors are simple enough to grok.