import java.awt.*;
import java.applet.Applet;
import java.text.DecimalFormat;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;

class CommonCellBuf 
{
    public static void main(String args[])
    {
        CatchInteger h=new CatchInteger();
        ProduceInteger p=new ProduceInteger(h);
        Consumeinteger c= new Consumeinteger(h);
        p.start();
        c.start();
    }
}

class ProduceInteger extends Thread
{
    private CatchInteger pTut;
    private TextArea cikti;

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

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

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

    }

class Consumeinteger extends Thread
{
    private CatchInteger cTut;

    public Consumeinteger(CatchInteger h)
    {
        cTut=h;
    }

    public void run()
    {
        int intValue;

        intValue=cTut.getCommonInteger();

        while (intValue !=9)
        {
            try{
                sleep( (int) (Math.random()*500));
               }
            catch(InterruptedException e)
            {
                System.err.println("Error "+e.toString());
            }
            intValue=cTut.getCommonInteger();
            System.out.println("ConsumeInteger caught "+intValue);

       }
    }
}

class CatchInteger
{
    private int Commonint[] = {9,9,9,9,9};
    private boolean writeable = true;
    private boolean readable = false;
    private int LocalRead =0, LocalPrint=0;
    public synchronized void setCommonInteger(int intValue)
    {
        while (!writeable)
        {
          try{
            wait();
             }
          catch (InterruptedException e)
          {
            System.err.println("Error :"+e.toString() );
          }
        }

        Commonint[LocalPrint]=intValue;
        readable = true;
        LocalPrint = ++LocalPrint % 5;       
        System.out.println("Produced Integer : "+intValue);
        printBuffer(Commonint);
        if(LocalPrint==LocalRead)
        {
            writeable=false;
            System.out.println("\nSTORAGE SPACE ˜ FULL ");
        }
        //notify();
    }
    
    public synchronized int getCommonInteger()
    {
        int intValue;
         while (!readable)
        {
          try{
            System.out.println(" WAITING TO BE USED ");
            wait();
             }
          catch (InterruptedException e)
          {
            System.err.println("Error:"+e.toString() );
          }
        }

        writeable=true;
        intValue=Commonint[LocalRead];
        LocalRead = ++LocalRead % 5;
        if (LocalRead==LocalPrint)
        {readable=false;
        System.out.println("\nINTEGER STRORAGE EMPTY");
        }
        notify();
        return intValue;
    }

    public void printBuffer(int buf[])
    {
	    String s="";
        s+="\t storage: ";
        for(int i=0; i<buf.length; i++)
           s+="  "+buf[i];
        System.out.println(s);   
    }
}
