title: How to run arbitrary code when the first item is placed into a java ExecutorService thread queue and when the last item is removed?
body: <p>I have an Executor (obtained via <code>Executors.newFixedThreadPool(int)</code>).  I would like to run some arbitrary code whenever a) the queue is empty and an item is inserted into the queue, and b) the queue is non-empty and the last item in the queue is removed.  Both a) and b) should always be run whenever their respective conditions are met, eg. if the queue is emptied and filled multiple times, a) and b) will be run multiple times.</p>
  
  <p>All of this should be invisible to the user of the executor, which implies the code should probably be inside some sort of wrapper that implements ExecutorService.  I can wrap the result of <code>newFixedThreadPool()</code> before returning it to the user, if I want.  I can also implement my own ExecutorServicee entirely if necessary.</p>
  
  <p>I want to be absolutely certain that a) and b) behave properly with respect to synchronization across multiple threads.  It's not clear to me the best way to do this.</p>
  
  <p>Suggestions?</p>