hi,
i have just started learning java threads. i have been given the task of solving dinning philosopher's problem by using only wait() and notify() methods. i cant use semaphores or monitors.
i have created philosophers as 5(0-4) threads and forks as objects upon which wait() and notify() methods can b called. i have writetn the code which i m posting 4 ur kind consideraton. my problem is that 0,2 and 4 philosophers are switching forks among them and the remaining 2 threads(1,3) are suffering 4m starvation. i dont know y?????
can anyone plz tell me that what i m doing wrong in this code and how its solution can be developed such that there is no deadlock,starva tion or livelock.
plz plz plz help me.....
P.java(Philosop her's class)
public class P implements Runnable
{
String n;
F f1,f2;
Thread t;
int s=0;
int r=0;
public P(F ff1,F ff2,String name)
{
n=name;
f1=ff1;
f2=ff2;
t=new Thread(this,n);
t.start();
}
public void run()
{
for(int i=0;i<4;i++)
{
r=f1.get(); // get 1st fork
if(r==5) // if 1st fork has been acquired, then request 2nd one
s=f2.get();
f1.eat(n);
f1.left(n,f2); // place tae 2 forks back on table
try
{Thread.sleep(5 0);}
catch(Exception e)
{System.out.pri ntln(e);}
}// end for
}// end run
}
T.java(Main class)
public class T
{
public static void main(String args[])
{
F f[]=new F[5];
P p[]=new P[5];
for(int i=0;i<5;i++)
{
f[i]=new F();
}// end for
for(int i=0;i<5;i++)
{
String name="Philosoph er # "+i;
if(i==0)
{p[0]=new P(f[4],f[i],name);} // sharing 2 fork objects with every philosopher
else
{p[i]=new P(f[i-1],f[i],name);}
}
}// end main
}//end class
F.java(Forks class)
public class F
{
private int fs=1; //represents availablity of forks
//if fs=1 -----> fork is available
//if fs=-1 -----> fork is not available
public F() //constructor
{
fs=1; // making all forks available at start of the program
}
public synchronized int get()
{
while(fs==-1)
{
try{wait();}
catch(Exception e)
{System.out.pri ntln(e);}
}//end while
fs=-1;
notifyAll();
return(5);
}// end get
public synchronized void eat(String h)
{
System.out.prin tln(h+" is eating...");
try{Thread.slee p(1300);}
catch(Exception e)
{System.out.pri ntln(e);}
}// end eat
public synchronized void left(String h,F f2)
{
System.out.prin tln(h+ " has finished eating ....");
this.fs=1;
notifyAll();
f2.fs=1;
notifyAll();
}// end left
}
i have just started learning java threads. i have been given the task of solving dinning philosopher's problem by using only wait() and notify() methods. i cant use semaphores or monitors.
i have created philosophers as 5(0-4) threads and forks as objects upon which wait() and notify() methods can b called. i have writetn the code which i m posting 4 ur kind consideraton. my problem is that 0,2 and 4 philosophers are switching forks among them and the remaining 2 threads(1,3) are suffering 4m starvation. i dont know y?????
can anyone plz tell me that what i m doing wrong in this code and how its solution can be developed such that there is no deadlock,starva tion or livelock.
plz plz plz help me.....
P.java(Philosop her's class)
public class P implements Runnable
{
String n;
F f1,f2;
Thread t;
int s=0;
int r=0;
public P(F ff1,F ff2,String name)
{
n=name;
f1=ff1;
f2=ff2;
t=new Thread(this,n);
t.start();
}
public void run()
{
for(int i=0;i<4;i++)
{
r=f1.get(); // get 1st fork
if(r==5) // if 1st fork has been acquired, then request 2nd one
s=f2.get();
f1.eat(n);
f1.left(n,f2); // place tae 2 forks back on table
try
{Thread.sleep(5 0);}
catch(Exception e)
{System.out.pri ntln(e);}
}// end for
}// end run
}
T.java(Main class)
public class T
{
public static void main(String args[])
{
F f[]=new F[5];
P p[]=new P[5];
for(int i=0;i<5;i++)
{
f[i]=new F();
}// end for
for(int i=0;i<5;i++)
{
String name="Philosoph er # "+i;
if(i==0)
{p[0]=new P(f[4],f[i],name);} // sharing 2 fork objects with every philosopher
else
{p[i]=new P(f[i-1],f[i],name);}
}
}// end main
}//end class
F.java(Forks class)
public class F
{
private int fs=1; //represents availablity of forks
//if fs=1 -----> fork is available
//if fs=-1 -----> fork is not available
public F() //constructor
{
fs=1; // making all forks available at start of the program
}
public synchronized int get()
{
while(fs==-1)
{
try{wait();}
catch(Exception e)
{System.out.pri ntln(e);}
}//end while
fs=-1;
notifyAll();
return(5);
}// end get
public synchronized void eat(String h)
{
System.out.prin tln(h+" is eating...");
try{Thread.slee p(1300);}
catch(Exception e)
{System.out.pri ntln(e);}
}// end eat
public synchronized void left(String h,F f2)
{
System.out.prin tln(h+ " has finished eating ....");
this.fs=1;
notifyAll();
f2.fs=1;
notifyAll();
}// end left
}
Comment