title: buffer getting fully filled before consuming
body: <pre><code>import java.awt.List;
  import java.util.ArrayList;
  import java.util.Random;enter code here
  
  public class shared {
  private int [] buffer;
  private int capacity;
  Object lock;//=new Object();
  int count=0;
  int i,j;
  //int i=0,j=0;
  
  public shared(int capacity){
      this.capacity = capacity;
      buffer = new int[capacity];
       i=0;
       j=0;
       lock = new Object();
      //for(int k=0;k&lt;capacity;k++){
          //buffer[k]=0;
      //}
  }
  
  public void producer() throws InterruptedException {
      //synchronized(lock){
      while(true){
          synchronized(lock){
      Random rn = new Random();
      int number = rn.nextInt(100);
      Thread.sleep(1000);
  
      while(count==capacity){
          try{
              System.out.println("BUffer is full");
          lock.wait();
  
          }catch(InterruptedException e){
  
          }
  
          //System.out.println("buffer is full");
      }
      System.out.println("producing" + number);
      buffer[i] = number;
      i = (i+1)%capacity;
      count++;
      lock.notifyAll();
  }
      }
  }
  
  
  public void consumer(){
      //synchronized(lock){
      while(true){
          synchronized(lock){
      while(count==0){
          try{
              lock.wait();
          }catch(InterruptedException e){
  
  
  
          }
      }
          //int a = buffer.get(0);
          //buffer.remove(0);
          int consumed = buffer[j];
          System.out.println("consuming" + consumed);
          j=(j+1)%capacity;
          count--;
          lock.notifyAll();
          if((consumed%2)==0){
              System.out.println("the number displayed by the consumer is " + consumed);
              }
      }
      }
  }
  }
  
  public class producer implements Runnable {
  shared h;
  
  
  public producer(shared s) {
      // TODO Auto-generated constructor stub
      h=s;
  }
  
  public void run(){
      //for(int i=0;i&lt;10;i++){
      try {
          h.producer();
      } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
  }
  
  }
  public class consumer implements Runnable {
  shared h;
  
      public consumer(shared s) {
          // TODO Auto-generated constructor stub
          h=s;
      }
      public void run(){
          //for(int i=0;i&lt;5;i++){
          h.consumer();
  
      }
      }
      public class implement {
      public static void main(String [] args) throws InterruptedException{
          shared s = new shared(10);
          Thread pro1 = new Thread(new producer(s));
          Thread con1 = new Thread(new consumer(s));
          pro1.start();
          con1.start();
          pro1.join();
          con1.join();
      }
      }
  </code></pre>
  
  <p>This is the entire code that I have written. The problem that I am getting is that my buffer is getting entirely full first then it is getting consumed. I want it to be consumed randomly i.e. when the first element fills up it might get get consumed or after 3-4 elements filled up.</p>