title: Terminating out of Producer Consumer setup using wait and notify
body: <p>Please find below consumer producer code:</p>
  
  <p>// Producer</p>
  
  <pre><code>    public boolean busy = false;
      while (rst != null &amp;&amp; rst.next()) {
          while (queue.size() == 10) {
              synchronized (queue) {
                  try {
                      log.debug("Queue is full, waiting");
                      queue.wait();
                  } catch (InterruptedException ex) {
                      ex.printStackTrace();
                  }
              }
          }
  
          synchronized (queue) {
              queue.add(/* add item to queue */);
              busy = true;
              queue.notifyAll();
          }
      }
  </code></pre>
  
  <p>// Consumer</p>
  
  <pre><code>    try {
          while (!busy) {
              while (queue.isEmpty()) {
                  synchronized (queue) {
                      try {
                          log.debug("Queue is empty, waiting");
                          queue.wait();
                      } catch (InterruptedException ex) {
                          ex.getMessage();
                      }
                  }
              }
  
              synchronized (queue) {
                  item = queue.remove();
                  log.debug("Consumed item" + ++count + " :" + item);
                  busy = false;
                  queue.notifyAll();
              }
          }
  </code></pre>
  
  <p>In my producer code snippet i have synchronized the queue(linked blocking queue) which i am using to add elements and made the global boolean variable busy to true  and notify consumer. Once queue size is 10 producer releases object lock and goes to wait state.
  For my consumer once global flag busy is true,consumer consumes elements and turn flag to false.
  The elements are produced and consumed appropriately.But my code is not terminating out of the producer consumer loop and final statement to be executed is "Queue is empty,waiting".
  Please let me know how to modify my code and termination condition to exit out of loop.</p>