title: Consumer-Producer Problem using multi-threading
body: <p>I'm pretty new to threads and just trying to get a grip on the basics.I'm trying to implement consumer-producer problem.</p>
  
  <p>Can anybody help me with the code.</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  
  
  public class T
  {
      public static void main(String[] args) {
  
          ConsumeProduce consumeproduce = new ConsumeProduce();
  
          C c=new C();
          P p=new P();
  
          c.start();
          p.start();
  
          consumeproduce.printList();
  
      }
  
  }
  
  
  class C extends Thread
  {
  
      public void run()
      {
          ConsumeProduce consumeproduce =new ConsumeProduce();
  
          try 
          {
              consumeproduce.consume();
  
          } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  }
  
  
  class P extends Thread
  {
      public void run()
      {
  
          ConsumeProduce consumeproduce =new ConsumeProduce();
  
          try {
              consumeproduce.produce();
  
  
          } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  }
  
  
  class ConsumeProduce
  {
  
      static int i=5;
      public List list =new ArrayList&lt;&gt;(10);
  
  
      synchronized void consume() throws InterruptedException
      {
          while(i&gt;0)
          {
          wait();
          list.remove(i);
          System.out.println("removed"+i);
          i--;
          notifyAll();
          }
      }
  
      synchronized void produce() throws InterruptedException
      {
          while(i&lt;=10)
          {
          wait();
          i++;
          list.add(i, 1);
          System.out.println("added"+i);
          notifyAll();
          }
      }
  
      void printList()
      {
          for (Object i:list)
          {
              System.out.println(i);
          }
      }
  
  }
  </code></pre>
  
  <p>I'm unable to figure what is wrong with my code.Any sort of help would be useful.</p>