title: Producer / Consumer using wait and notify
body: <p>I am trying to coordinate a producer and consumer thread problem. The producer puts 10 objects on the queue. That is the producer's run method just includes a simple loop to add 10 objects then it is finished. When the queue is full (queue is size 10) there is a call to wait()  - > in the queue's add method. On the consumer side, the consumer starts by peeking at the objects and then it starts removing them. The problem I am having is that in roughly 50% of cases when the program is ran, the output terminates after the producer puts the 10 objects on queue. The other 50% of times the program works fine - that is the consumer takes off all the objects. My prior solution to this problem was to create a new consumer thread within the producer's run method. So once the producer had put ten objects on queue, the new consumer thread was created and I used join() to synchronize operations. However I would like to get the process working with wait and notify. Can someone please advise what I am doing wrong? thanks</p>
  
  <pre><code>    @Override
      public synchronized boolean add(Process element) {
      if(isFull())
      {
          waitForNotify();
      }
  
      else
      {          
  
         queue.add(element);
  
  
  
      }
      return true;
  }
  
      private void invokeNotify() {
      try {
          notify();
      } catch (Exception e) {
          e.printStackTrace();
      }
  
  }
  
      private void waitForNotify() {
      try {
          wait();
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
  
  }
  
       @Override
       public synchronized boolean offer(Process element) throws IllegalStateException {
         queue.add(element);
         this.queue = newHeapSort.heapify(queue, queue.size());
         return true;
  
  
  
  }
  
      @Override
      public synchronized Process peek() {
      if(queue.isEmpty())
      {
          waitForNotify();
      }
      return(queue.get(0));
  
  }
  
      @Override
      public synchronized Process head() {    
      if(queue.isEmpty()) 
      {
          invokeNotify();
      }
  
          Process head = queue.get(0);
          queue.remove(0);
  
  
          return head;
  
  
  }
  </code></pre>