title: Being asynchronously notified of a BlockingQueue having an item available
body: <p>I need an <code>Object</code> to be asynchronously notified when some <code>BlockingQueue</code> has got an item to give.</p>
  
  <p>I've searched both Javadoc and the web for a pre-made solution, then I ended up with a (maybe naive) solution of mine, here it is:</p>
  
  <pre><code>interface QueueWaiterListener&lt;T&gt; {
      public void itemAvailable(T item, Object cookie);
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class QueueWaiter&lt;T&gt; extends Thread {
  
      protected final BlockingQueue&lt;T&gt; queue;
      protected final QueueWaiterListener&lt;T&gt; listener;
      protected final Object cookie;
  
      public QueueWaiter(BlockingQueue&lt;T&gt; queue, QueueWaiterListener&lt;T&gt; listener, Object cookie) {
          this.queue = queue;
          this.listener = listener;
          this.cookie = cookie;
      }
  
      public QueueWaiter(BlockingQueue&lt;T&gt; queue, QueueWaiterListener&lt;T&gt; listener) {
          this.queue = queue;
          this.listener = listener;
          this.cookie = null;
      }
  
      @Override
      public void run() {
          while (!isInterrupted()) {
              try {
                  T item = queue.take();
                  listener.itemAvailable(item, cookie);
              } catch (InterruptedException e) {
              }
          }
      }
  }
  </code></pre>
  
  <p>Basically, there's a thread blocking on a <code>take()</code> operation of a queue that callbacks a listener object everytime a <code>take()</code> operation succeeds, optionally sending back a special <code>cookie</code> object (ignore it if you want).</p>
  
  <p>Question is: is there any better way to do this? Am I doing some unforgivable mistake (both in concurrency/efficiency and/or code cleanness)? Thanks in advance.</p>