title: Producer-Consumer using a stack in Java
body: <p>I am using a stack as my storage to implement my Producer-Consumer model.</p>
  
  <pre><code>public class Main {
  
  /**
   * @param args
   */
  public static void main(String[] args) {
      Stackable&lt;Integer&gt; stack = new MyArrayStack&lt;Integer&gt;();
      Producer producer = new Producer(stack);
      Consumer consumer = new Consumer(stack);
      producer.start();
      consumer.start();
  }
  
  private static class Producer extends Thread {
      public Producer(Stackable&lt;Integer&gt; s) {
          mStack = s;
      }
  
      private Stackable&lt;Integer&gt; mStack = null;
      private int mNumber = 0;
  
      @Override
      public void run() {
          // TODO generates number here
          while(true){        synchronized(this){
                  while(!mStack.isEmpty())
                  {
                      try{
                          this.wait();
                      } catch(Exception e)
                      {
                          e.printStackTrace();
                      }
                  }
              mNumber++;
              System.out.println("Producer generates number:" + mNumber);
              mStack.push(mNumber);
              this.notifyAll();
              }
          }   
      }
  }
  
  private static class Consumer extends Thread {
      public Consumer(Stackable&lt;Integer&gt; s) {
          mStack = s;
      }
  
      private Stackable&lt;Integer&gt; mStack = null;
  
      @Override
      public void run() {
          // TODO consume number here.
          while(true){
              synchronized(this){
                  while(mStack.isEmpty())
                  {
                      try{
                          this.wait();
                      } catch(Exception e)
                      {
                          e.printStackTrace();
                      }
                  }
              int number = mStack.pop();
              System.out.println("Consumer consumes number:" + number);
              this.notifyAll();
              }
          }}
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Howver, when I test the program, it seems only the producer will work, it keeps generating numbers while consumer thread seems not be working.</p>
  
  <p>Could expert help me debug where I went wrong in my code? Thanks.</p>
  
  <p>Edit: My code for  my stack is :
      public class MyArrayStack implements Stackable {</p>
  
  <pre><code>private static final int DEFAULT_SIZE = 16;
  protected int sp; // empty stack
  protected E[] head; // array
  private int size;
  private int count;
  
  MyArrayStack(int size) {
      if (size &lt;= 0)
            throw new IllegalArgumentException(
                       "Stack's capacity must be positive");
      head = (E[])new Object[size];
      sp = -1;
      count=0;
  
  }
  
  
  
  
  
  
  public boolean isFull() {
      return sp == this.size -1;
  }
  
  @Override
  public void push(Object e) {
  
      if (!isFull())
      {
          head[++sp] = (E) e;
          count++;
      }
  
  }
  
  @Override
  public E pick() {
      if (sp == -1)
          try {
              throw new Exception("Stack is empty");
          } catch (Exception e) {
  
              e.printStackTrace();
          }
      return head[sp];
  }
  
  @Override
  public E pop() {
      count--;
      if (isEmpty()) {
          return null;
      } else
          return head[sp--];
  
  }
  
  @Override
  public int count() {
  
      return count;
  }
  
  @Override
  public boolean isEmpty() {
  
      return (sp == -1);
  }
  }
  </code></pre>