/*ch1007.java*/
class datapull
{
  private int seq;
  private boolean available=false;
  public synchronized int get()
  {
    while(available==false)
    {
      try
      {
        wait();
      }
      catch(InterruptedException e)
      {
      }
    }
    available=false;
    notify();
    return seq;
  }
  public synchronized void put(int value)
  {
    while(available==true)
    {
      try
      {
        wait();
      }
      catch(InterruptedException e)
      {
      }
    }
    seq=value;
    available=true;
    notify();
  }
}

class Producer extends Thread
{
  private datapull data;
  private int number;
  public Producer(datapull c,int num)
  {
    data=c;
    number=num;
  }
  public void run()
  {
    for(int i=0;i<5;i++)
    {
      data.put(i);
      System.out.println("Producer # "+number+"put: "+i);
      try
      {
        sleep((int)(Math.random()*100));
      }
      catch(InterruptedException e)
      {
      }
    }
  }
}

class Consumer extends Thread
{
  private datapull data;
  private int number;
  public Consumer(datapull c,int num)
  {
    data=c;
    number=num;
  }
  public void run()
  {
    int value=0;
    for(int i=0;i<5;i++)
    {
      value=data.get();
      System.out.println("Consumer # "+number+"get:   "+value);
    }
  }
}

public class ch1007
{
  public static void main(String arg[])
  {
    datapull data=new datapull();
    Producer p1=new Producer(data,1);
    Consumer c1=new Consumer(data,1);
    p1.start();
    c1.start();
  }
}