title: Java ExecutorService: awaitTermination of all recursively created tasks
body: <p>I use an <code>ExecutorService</code> to execute a task. This task can recursively create other tasks which are submitted to the same <code>ExecutorService</code> and those child tasks can do that, too.</p>
  
  <p>I now have the problem that I want to wait until all the tasks are done (that is, all tasks are finished and they did not submit new ones) before I continue.</p>
  
  <p>I cannot call <code>ExecutorService.shutdown()</code> in the main thread because this prevents new tasks from being accepted by the <code>ExecutorService</code>. </p>
  
  <p>And Calling <code>ExecutorService.awaitTermination()</code> seems to do nothing if <code>shutdown</code> hasn't been called.</p>
  
  <p>So I am kinda stuck here. It can't be that hard for the <code>ExecutorService</code> to see that all workers are idle, can it? The only inelegant solution I could come up with is to directly use a <code>ThreadPoolExecutor</code> and query its <code>getPoolSize()</code> every once in a while. Is there really no better way do do that?</p>