title: Wait for all tasks (unknown number) to finish - ThreadPoolExecutor
body: <p>I ran into this concurrency problem which I've been scratching my head for several days.</p>
  
  <p>Basically, I want my ThreadPoolExecutor to wait for all tasks (number of tasks unknown) to finish before shutting down.</p>
  
  <pre><code>public class AutoShutdownThreadPoolExecutor extends ThreadPoolExecutor{
      private static final Logger logger = Logger.getLogger(AutoShutdownThreadPoolExecutor.class);
      private int executing = 0;
      private ReentrantLock lock = new ReentrantLock();
      private final Condition newTaskCondition = lock.newCondition(); 
      private final int WAIT_FOR_NEW_TASK = 120000;
  
      public AutoShutdownThreadPoolExecutor(int coorPoolSize, int maxPoolSize, long keepAliveTime,
          TimeUnit seconds, BlockingQueue&lt;Runnable&gt; queue) {
          super(coorPoolSize, maxPoolSize, keepAliveTime, seconds, queue);
      }
  
  
      @Override
      public void execute(Runnable command) {
          lock.lock();
          executing++;
          lock.unlock();
          super.execute(command);
      }
  
  
      @Override
      protected void afterExecute(Runnable r, Throwable t) {
          super.afterExecute(r, t);
          try{
              lock.lock();
              int count = executing--;
              if(count == 0) {
                  newTaskCondition.await(WAIT_FOR_NEW_TASK, TimeUnit.MILLISECONDS); 
                  if(count == 0){
                      this.shutdown();
                      logger.info("Shutting down Executor...");
                  }
              }
          }
          catch (InterruptedException e) {
              logger.error("Sleeping task interrupted", e);
          }
          finally{
              lock.unlock();
          }
      }
  }
  </code></pre>
  
  <p>The intention is that the task checks for the task counter (executing), and if it equals 0, it blocks for a while, and later releases its lock so that other tasks may have the chance to execute and not shutting down the executor too soon.</p>
  
  <p>However, it is not happening. All 4 threads in the executor go into the waiting state:</p>
  
  <pre><code>"pool-1-thread-4" prio=6 tid=0x034a1000 nid=0x2d0 waiting on condition [0x039cf000]
  "pool-1-thread-3" prio=6 tid=0x034d0400 nid=0x1328 waiting on condition [0x0397f000]
  "pool-1-thread-2" prio=6 tid=0x03493400 nid=0x15ec waiting on condition [0x0392f000]
  "pool-1-thread-1" prio=6 tid=0x034c3800 nid=0x1fe4 waiting on condition [0x038df000]
  </code></pre>
  
  <p>If I put a log statement(supposed to slow the thread down) in the Runnable class, the problem seems to disappear.</p>
  
  <pre><code>public void run() {
      //  logger.info("Starting task" + taskName);
          try {
              //doTask();
          }
          catch (Exception e){
              logger.error("task " + taskName + " failed", e);
          }
  }
  </code></pre>
  
  <p>The question is similar to this post 
  <a href="https://stackoverflow.com/questions/4958330/java-executorservice-awaittermination-of-all-recursively-created-tasks">Java ExecutorService: awaitTermination of all recursively created tasks</a></p>
  
  <p>I have adopted the original poster solution and attempted to address the race condition in afterExecute() but it doesn't work.</p>
  
  <p>Please help shed some light to this.
  Thank you.</p>