Flexible CountDownLatch?
I have encountered a problem twice now whereby a producer thread produces N work items, submits them to an ExecutorService and then needs to wait until all N items have been processed. Caveats N is not known in advance . If it were I would simply create a CountDownLatch and then have producer thread await() until all work was complete. Using a CompletionService is inappropriate because although my producer thread needs to block (i.e. by calling take() ) there's no way of signalling that all work is complete , to cause the producer thread to stop waiting. My current favoured solution is to use