Tuesday, 10 May 2011

Reader Writer Algorithm...


/*******************************************************************************
By => Anand Mahuli...  Xtream Kings... K.K.Wagh COE,Nashik...
           Anand's Blog :- http://designyourdreamshere.blogspot.com/
*******************************************************************************/
class ww extends Thread{
    ww()
    {
    start();
    }
public void run()
    {
if(RW1.sem!=3)
        {
        System.out.println("Writer(i="+RW1.i+") (Thread : "+Thread.currentThread().getId()+") Waiting...");  
            try{sleep((int)(Math.random()*1000));}catch(Exception e){}
        }
        else
        {
        RW1.sem=2;
        System.out.println("Writer(i="+RW1.i+") (Thread : "+Thread.currentThread().getId()+") Writing...");
            try{sleep((int)(Math.random()*10000));}catch(Exception e){}
            RW1.sem=3;
            System.out.println("Writer(i="+RW1.i+") (Thread : "+Thread.currentThread().getId()+") Done Writing...");
        }
    }
}

class rr extends Thread{
    rr()
    {
    start();
    }
public void run()
    {
if(RW1.sem==2)
        {
System.out.println("Reader(i="+RW1.i+") (Thread : "+Thread.currentThread().getId()+") Waiting...");            
            try{sleep((int)(Math.random()*1000));}catch(Exception e){}
        }
        else
        {
        RW1.sem=1;
        System.out.println("Reader(i="+RW1.i+") (Thread : "+Thread.currentThread().getId()+") Reading...");
        try{sleep((int)(Math.random()*1000));}catch(Exception e){}
        RW1.sem=3;
        System.out.println("Reader(i="+RW1.i+") (Thread : "+Thread.currentThread().getId()+") Done Reading...");
        }
    }
}
public class RW1 extends Thread
{
    public static int sem=3,i;
   
    public static void main(String args[])
    {
    int j;
        rr threadr[] = new rr[5];
        ww threadw[] = new ww[5];
     
        for(i=0;i<3;i++)
        {
        System.out.println(i);
        try {sleep(1000);} catch(Exception e) {}
        j=(int)(Math.random()*3);
        //this is the key area in this program...i used  a random switch so as two create random no of reader write at a paritcular moment
        switch(j)
        {
        case 0:
        threadw[i]=new ww();
        System.out.println("Writer(i="+i+") Thread Created : "+threadw[i].getId());
        threadw[i]=new ww();
        System.out.println("Writer(i="+i+"(2)) Thread Created : "+threadw[i].getId());
       
        break;
        case 1:
        threadr[i]=new rr();
        System.out.println("Reader(i="+i+") Thread Created : "+threadr[i].getId());
        threadr[i]=new rr();
        System.out.println("Reader(i="+i+"(2)) Thread Created : "+threadr[i].getId());
       
        break;
        case 2:
        threadw[i]=new ww();
        System.out.println("Writer(i="+i+") Thread Created : "+threadw[i].getId());
        threadr[i]=new rr();
        System.out.println("Reader(i="+i+") Thread Created : "+threadr[i].getId());
        break;
        case 3:
        threadr[i]=new rr();
        System.out.println("Reader(i="+i+") Thread Created : "+threadr[i].getId());
        threadw[i]=new ww();
        System.out.println("Writer(i="+i+") Thread Created : "+threadw[i].getId());
        break;
  }
        }
        System.out.println("Exit From Main Thread ");
}
}

/*
 0
Writer(i=0) Thread Created : 8
Writer(i=0(2)) Thread Created : 9
1
Writer(i=0) (Thread : 8) Writing...
Writer(i=0) (Thread : 9) Waiting...
Reader(i=1) Thread Created : 10
Reader(i=1(2)) Thread Created : 11
2
Reader(i=0) (Thread : 10) Waiting...
Reader(i=0) (Thread : 11) Waiting...
Writer(i=0) (Thread : 8) Done Writing...
Writer(i=2) Thread Created : 12
Reader(i=2) Thread Created : 13
Exit From Main Thread
Writer(i=0) (Thread : 12) Writing...
Reader(i=0) (Thread : 13) Waiting...
Writer(i=0) (Thread : 12) Done Writing...

 */

No comments:

Post a Comment