class OrtakHucreSyn
{
    public static void main(String args[])
    {
        TutInteger h=new TutInteger();
        UretInteger p=new UretInteger(h);
        KullanInteger c= new KullanInteger(h);

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

class UretInteger extends Thread
{
    private TutInteger pTut;

    public UretInteger(TutInteger h)
    {
        pTut=h;
    }

    public void run()
    {
        for(int count=0; count<10; count++)
        {
            pTut.setOrtakInt(count);
            System.out.println("UretInteger OrtakInteger degerini"+count+"olarak olusturdu");

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

class KullanInteger extends Thread
{
    private TutInteger cTut;

    public KullanInteger(TutInteger h)
    {
        cTut=h;
    }

    public void run()
    {
        int Intedegeri=0;

        while (Intedegeri !=9)
        {
            try{
                sleep( (int) (Math.random()*3000));
               }
            catch(InterruptedException e)
            {
                System.err.println("Exeption "+e.toString());
            }
            Intedegeri=cTut.getOrtakInt();
            System.out.println("KullanInteger "+Intedegeri+" yakalad¨");
       }
    }
}

class TutInteger
{
    private int OrtakInt;
    private boolean Yazilabilir = true;


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

        OrtakInt=Intedegeri;
        Yazilabilir=false;
        notify();

    }

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


        Yazilabilir=true;
        notify();

        return OrtakInt;
    }
}

