This is an abstraction of my actual problem, but I hope it's accurate enough to explain things.
I'm processing a file hierarchy, and I'm processing the files asynchronously using a Java ThreadPoolExecutor with a finite number of threads and an unbounded queue. Starting at a particular directory, I add a task to the queue for each file within the directory. Then I shutdown and await completion.
The tasks are despatched using Executor.execute(Runnable).
The problem is that when the directory contains further directories, one of these tasks may spawn further tasks, and these tasks are not being executed because at the top level the ThreadPoolExecutor has already been shut down.
So my question is, in my top level thread, how do I await completion of the whole hierarchy of tasks, recognising that they haven't actually all started yet?
I did say that this is an abstraction of the problem. If it were exactly as described, I could walk the whole hierarchy in the orginal parent thread and fire off all tasks from there. But in my real problem I can't do that: it's an essential feature of the problem that a spawned child task itself submits further tasks.
In your top level thread you can use something like that:
A CountDownLatch initialized to N can be used to make one thread wait until N threads have completed some action, or some action has been completed N times. http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html