title: Is there better way to use wait/notify with connection with AtomicInteger
body: <p>I've this kind of code:</p>
  
  <pre><code>public class RecursiveQueue {
      //@Inject
      private QueueService queueService;
      public static void main(String[] args) {
          RecursiveQueue test = new RecursiveQueue();
          test.enqueue(new Node("X"), true);
          test.enqueue(new Node("Y"), false);
          test.enqueue(new Node("Z"), false);
      }
  
      private void enqueue(final Node node, final boolean waitTillFinished) {
          final AtomicLong totalDuration = new AtomicLong(0L);
          final AtomicInteger counter = new AtomicInteger(0);
  
          AfterCallback callback= new AfterCallback() {
              @Override
              public void onFinish(Result result) {
                  for(Node aNode : result.getChildren())  {
                      counter.incrementAndGet();
                      queueService.requestProcess(aNode, this);
                  }
  
                  totalDuration.addAndGet(result.getDuration());
                  if(counter.decrementAndGet() &lt;= 0) { //last one
                      System.out.println("Processing of " + node.toString() + " has finished in " + totalDuration.get() + " ms");
                      if(waitTillFinished) {
                          counter.notify();
                      }
                  }
              }
          };
  
          counter.incrementAndGet();
          queueService.requestProcess(node, callback);
          if(waitTillFinished) {
              try {
                  counter.wait();
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>Imagine there is a <code>queueService</code> which uses blocking queue and few consumer threads to process nodes = calls DAO to fetch children of nodes (it's a tree).
  So <code>requestProcess</code> method just enqueues the node and does not block.</p>
  
  <p>Is there some better/safe way to avoid using wait/notify in this sample ?
  According to some findings I can use Phaser (but I work on java 6) or conditions (but I'm not using locks).</p>