If waiting for all tasks in the ExecutorService
to finish isn't precisely your goal, but rather waiting until a specific batch of tasks has completed, you can use a CompletionService
— specifically, an ExecutorCompletionService
.
The idea is to create an ExecutorCompletionService
wrapping your Executor
, submit some known number of tasks through the CompletionService
, then draw that same number of results from the completion queue using either take()
(which blocks) or poll()
(which does not). Once you've drawn all the expected results corresponding to the tasks you submitted, you know they're all done.
Let me state this one more time, because it's not obvious from the interface: You must know how many things you put into the CompletionService
in order to know how many things to try to draw out. This matters especially with the take()
method: call it one time too many and it will block your calling thread until some other thread submits another job to the same CompletionService
.
There are some examples showing how to use CompletionService
in the book Java Concurrency in Practice.