Search code examples
javamulticoreexecutorservicefork-join

Is there an equivalent of isQuiescent in non-fork-join executor services in Java?


I have made two implementations of my problem in ForkJoin and FixedSizeThreadPool and I want to compare their performances. In my problem, each task will create some sub-tasks and will submit them to the executor service again. In the fork-join implementation, I use fork() and in the other one, I use execute().

Obviously, my main thread needs to wait until the whole work has finished. In fork-join, I can gracefully check the isQuiescent() property to make sure everything has finished, but in the other case, I cannot think of anything better than manually counting how many tasks I have created and how many have finished! This solution works, but at the cost of a shared variable that obviously reduces my performance.

I cannot just use shutdown(), because then the sub-tasks will not be scheduled (they will get RejectedExecutionException).

Does anybody know a better solution than counting the tasks?


Solution

  • Actually shutdown waits until all the previously submitted tasks are finished. For your use case look at ExecutorCompletionService.