import java.util.Random;
class CommonCell
{
    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 pCatch;
    int total;

    public ProduceInteger(CreateInteger h)
    {
        pCatch=h;
        total=0;
    }

    public  void run() 
    {
        for(int count=0; count<10; count++)
        {
            pCatch.setCommonint(count);
            total+=count;
            System.out.println("Produce integer produced  "+count+"  Total produced = "+total);

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

class ConsumeInteger extends Thread
{
    private CreateInteger cCatch;
    int total;
    
    public ConsumeInteger(CreateInteger h)
    {
        cCatch=h;
    }

    public void run() 
    {
        int intvalue=0;


        while (intvalue !=9)
        {
            try{
                sleep( (int) (Math.random()*5000));
               }
            catch(InterruptedException e)
            {
                System.err.println("Exeption "+e.toString());
            }
        intvalue=cCatch.getCommonInteger();
        total+=intvalue;
        System.out.println("Consume integer catched : "+intvalue+"  Total consumed = "+total);

       }
    }
}

class CreateInteger
{
    private int Commonint;

    public  void setCommonint(int intvalue)
    {
        Commonint=intvalue;
    }

    public int getCommonInteger()
    {
        return Commonint;
    }
}
