class Producer extends Thread {
private String [] buffer = new String[8];
  private int pi = 0 ; //produce index;
  private int gi = 0; //get index

  public void run () {
    for(int i=0;i<10;i++) 
      produce();
  }

private final long start = System.currentTimeMillis();
  private final String banana() {
    return " " +(int) (System.currentTimeMillis() - start);
  }

  synchronized void produce() {
    //while no room in buffer
    while (pi-gi+1 > buffer.length) 
      {
	try{wait();} catch(Exception e) {}
      }
    buffer[pi&0x7] = banana();
    System.out.println("produced["+(pi&7)+"] " + buffer[pi&7]);
    pi++;
    notifyAll();
  }
  synchronized String consume() {
    // While tehre is nothing left to take
    while (pi==gi) 
      {
	try{wait();} catch(Exception e) {}
      }
    notifyAll();
    return buffer[gi++&0x7] ;
  }
}

