title: ThreadPoolExecutor's getActiveCount()
body: <p>I have a ThreadPoolExecutor that seems to be lying to me when I call getActiveCount(). I haven't done a lot of multithreaded programming however, so perhaps I'm doing something incorrectly.</p>
  
  <p>Here's my TPE</p>
  
  <pre><code>@Override
  public void afterPropertiesSet() throws Exception {
  
      BlockingQueue&lt;Runnable&gt; workQueue;
      int maxQueueLength = threadPoolConfiguration.getMaximumQueueLength();
      if (maxQueueLength == 0) {
          workQueue = new LinkedBlockingQueue&lt;Runnable&gt;();
      } else {
          workQueue = new LinkedBlockingQueue&lt;Runnable&gt;(maxQueueLength);
      }
  
      pool = new ThreadPoolExecutor(
                     threadPoolConfiguration.getCorePoolSize(),
                     threadPoolConfiguration.getMaximumPoolSize(),
                     threadPoolConfiguration.getKeepAliveTime(),
                     TimeUnit.valueOf(threadPoolConfiguration.getTimeUnit()),
                     workQueue,
                     // Default thread factory creates normal-priority,
                     // non-daemon threads.
                     Executors.defaultThreadFactory(),
                     // Run any rejected task directly in the calling thread.
                     // In this way no records will be lost due to rejection
                     // however, no records will be added to the workQueue
                     // while the calling thread is processing a Task, so set
                     // your queue-size appropriately.
                     //
                     // This also means MaxThreadCount+1 tasks may run
                     // concurrently. If you REALLY want a max of MaxThreadCount
                     // threads don't use this.
                     new ThreadPoolExecutor.CallerRunsPolicy());
  }
  </code></pre>
  
  <p>In this class I also have a DAO that I pass into my Runnable (<code>FooWorker</code>), like so:</p>
  
  <pre><code>@Override
  public void addTask(FooRecord record) {
      if (pool == null) {
          throw new FooException(ERROR_THREAD_POOL_CONFIGURATION_NOT_SET);
      }
      pool.execute(new FooWorker(context, calculator, dao, record));
  }
  </code></pre>
  
  <p><code>FooWorker</code> runs <code>record</code> (the only non-singleton) through a state machine via <code>calculator</code> then sends the transitions to the database via <code>dao</code>, like so:</p>
  
  <pre><code>public void run() {
      calculator.calculate(record);
      dao.save(record);
  }
  </code></pre>
  
  <p>Once my main thread is done creating new tasks I try and wait to make sure all threads finished successfully:</p>
  
  <pre><code>while (pool.getActiveCount() &gt; 0) {
      recordHandler.awaitTermination(terminationTimeout, 
                                     terminationTimeoutUnit);
  }
  </code></pre>
  
  <p>What I'm seeing from output logs (which are presumably unreliable due to the threading) is that getActiveCount() is returning zero too early, and the while() loop is exiting while my last threads are still printing output from <code>calculator</code>.</p>
  
  <p>Note I've also tried calling <code>pool.shutdown()</code> then using <code>awaitTermination</code> but then the next time my job runs the pool is still shut down.</p>
  
  <p>My only <em>guess</em> is that inside a thread, when I send data into the <code>dao</code> (since it's a singleton created by Spring in the main thread...), java is considering the thread inactive since (<em>I assume</em>) it's processing in/waiting on the main thread.</p>
  
  <p>Intuitively, based only on what I'm seeing, that's my guess. But... Is that really what's happening? Is there a way to "do it right" without putting a manual incremented variable at the top of <code>run()</code> and a decremented at the end to track the number of threads? </p>
  
  <p>If the answer is "don't pass in the dao", then wouldn't I have to "new" a DAO for every thread? My process is already a (beautiful, efficient) beast, but that would really suck.</p>