class CommonCellSyn
{
    public static void main(String args[])
    {
        CreateInteger  h=new CreateInteger();
        ProduceInteger p=new ProduceInteger(h);
        ConsumeInteger c= new ConsumeInteger(h);
        p.start();
        c.start();
    }
}

class ProduceInteger extends Thread
{
    private CreateInteger pTut;

    public ProduceInteger(CreateInteger h)
    {
        pTut=h;
    }

    public void run()
    {
        for(int count=0; count<10; count++)
        {
            pTut.setCommonInt(count);
            System.out.println("ProduceInteger created  "+count);

           try{
                 sleep( (int) (Math.random()*3000 ) );
             }
             catch(InterruptedException e)
            {
               System.err.println("Exeption "+e.toString());
           }
        }
    }
}

class ConsumeInteger extends Thread
{
    private CreateInteger cTut;

    public ConsumeInteger(CreateInteger h)
    {
        cTut=h;
    }

    public void run()
    {
        int IntValue=0;

        while (IntValue !=9)
        {
            try{
                sleep( (int) (Math.random()*3000));
               }
            catch(InterruptedException e)
            {
                System.err.println("Exeption "+e.toString());
            }
            IntValue=cTut.getCommonInt();
            System.out.println("ConsumeInteger caught "+IntValue);
       }
    }
}

class CreateInteger
{
    private int CommonInt;
    private boolean Writable = true;


    public synchronized void setCommonInt(int IntValue)
    {
        while (!Writable)
        {
          try{
            wait();
             }
          catch (InterruptedException e)
          {
            System.err.println("Exception:"+e.toString() );
          }
        }

        CommonInt=IntValue;
        Writable=false;
        notify();

    }

    public synchronized int getCommonInt()
    {
         while (Writable)
        {
          try{
            wait();
             }
          catch (InterruptedException e)
          {
            System.err.println("Exception:"+e.toString() );
          }
        }


        Writable=true;
        notify();

        return CommonInt;
    }
}
