title: Elegantly implementing queue length indicators to ExecutorServices
body: <p>Why, oh why doesn't <code>java.util.concurrent</code> provide a queue length indicators for  its <code>ExecutorService</code>s? Recently I found myself doing something like this:</p>
  
  <pre><code>ExecutorService queue = Executors.newSingleThreadExecutor();
  AtomicInteger queueLength = new AtomicInteger();
  ...
  
  public void addTaskToQueue(Runnable runnable) {
      if (queueLength.get() &lt; MAX_QUEUE_LENGTH) {
          queueLength.incrementAndGet(); // Increment queue when submitting task.
          queue.submit(new Runnable() {
              public void run() {
                  runnable.run();
                  queueLength.decrementAndGet(); // Decrement queue when task done.
              }
          });
      } else {
          // Trigger error: too long queue
      }
  }
  </code></pre>
  
  <p>Which works ok, but... I think this really should be implemented as a part of the <code>ExecutorService</code>. It's dumb and error prone to carry around a counter <em>separated</em> from the actual queue, whose length the counter is supposed to indicate (reminds me of C arrays). But, <code>ExecutorService</code>s are obtained via static factory methods, so there's no way to simply extend the otherwise excellent single thread executor and add a queue counter. So what should I do:</p>
  
  <ol>
  <li>Reinvent stuff already implemented in JDK?</li>
  <li>Other clever solution?</li>
  </ol>