import java.awt.*;
import java.applet.Applet;
import java.text.DecimalFormat;

public class OrtakHucreBuf extends Applet
{
    private TextArea cikti;

    public void init()
    {
        setLayout(new BorderLayout());
        cikti = new TextArea();
        add(cikti,BorderLayout.CENTER);
    }


    public void start()
    {
        Tutinteger h=new Tutinteger(cikti);
        Uretinteger p=new Uretinteger(h);
        Consumeinteger c= new Consumeinteger(h);

        p.start();
        c.start();
    }
}

class Uretinteger extends Thread
{
    private Tutinteger pTut;
    private TextArea cikti;

    public Uretinteger(Tutinteger h)
    {
        pTut=h;
    }

    public void run()
    {
        for(int count=0; count<10; count++)
        {
            pTut.setOrtakint(count);

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

class Consumeinteger extends Thread
{
    private Tutinteger cTut;

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

    public void run()
    {
        int intdegeri;

        intdegeri=cTut.getOrtakint();

        while (intdegeri !=9)
        {
            try{
                sleep( (int) (Math.random()*500));
               }
            catch(InterruptedException e)
            {
                System.err.println("Hata "+e.toString());
            }
            intdegeri=cTut.getOrtakint();

       }
    }
}
class Tutinteger
{
    private int Ortakint[] = {9,9,9,9,9};
    private boolean writeable = true;
    private boolean okunabilir = false;
    private int YerelOku =0, YerelYaz=0;
    private TextArea cikti;

    public Tutinteger(TextArea out)
    {
        cikti = out;
    }

    public synchronized void setOrtakint(int intdegeri)
    {
        while (!writeable)
        {
          try{

            cikti.append(" Beklemede "+intdegeri);
            wait();
             }
          catch (InterruptedException e)
          {
            System.err.println("Hata :"+e.toString() );
          }
        }

        Ortakint[YerelYaz]=intdegeri;
        okunabilir = true;
        cikti.append("\n Uretilen  "+intdegeri+" Yazilan  "+YerelYaz);
        YerelYaz = ++YerelYaz % 5;

        cikti.append("\t yazilan hucre "+YerelYaz+"\t okunan hucre "+YerelOku);

        printBuffer(cikti, Ortakint);

        if(YerelYaz==YerelOku)
        {
            writeable=false;
            cikti.append("\nDEPOLAMA YERLER DOLU ");
        }
        notify();
    }

    public synchronized int getOrtakint()
    {
        int intdegeri;



         while (!okunabilir)
        {
          try{
            cikti.append(" KULLANMAK iCiN BEKLiYOR ");
            wait();
             }
          catch (InterruptedException e)
          {
            System.err.println("hata:"+e.toString() );
          }
        }

        writeable=true;
        intdegeri=Ortakint[YerelOku];

        cikti.append("\n Kullanilan "+intdegeri+" Okundugu Hucre "+YerelOku);
        YerelOku = ++YerelOku % 5;
        cikti.append("\t yazilan hcre "+YerelYaz+"\t okunan hucre "+YerelOku);

        printBuffer(cikti, Ortakint);
        if (YerelOku==YerelYaz)
        {okunabilir=false;
        cikti.append("\niNTEGER DEPOSU BOS");
        }


        notify();

        return intdegeri;
    }

    public void printBuffer(TextArea out,int buf[])
    {
        out.append("\t depo: ");
        for(int i=0; i<buf.length; i++)
           out.append("  "+buf[i]);
    }
}








