Dining Philosophers - problem with code!

Hello Experts,

The code below was supposed to be the "Dining Philosophers" problem ... but it does not.
Can anyone tell me what's going wrong and why?, and help me to make it work properly??

Thanks in advance!


class Philosopher extends Thread {
 int idNumber;
 Chopstick left; Chopstick right;
 
 Philosopher(int idNumber, Chopstick left, Chopstick right){
  this.idNumber = idNumber;
  this.left = left; this.right = right;
 }
 
 public void run() {
  for(int i = 0; i < 500; i++) {  
   try {
   
    System.out.println("Philosopher " + idNumber  
+ " thinks...");
    sleep(5);  
    System.out.println("Philosopher " + idNumber  
+ " is hungry...");
    right.get();  
    left.get();  
    System.out.println("Philosopher " + idNumber  
+ " eats...");
    sleep(5) ;  
    right.put();  
    left.put();
   
   } catch (InterruptedException e) {  
System.out.println(e);
}
  }
   
System.out.println("Philosopher number " + idNumber  
+ " died with a full stomach");  
// the successful end of this philosophers task
 }
}
 
class Chopstick {
 
 boolean taken=false;  
 
 synchronized void put() {  // drop the chopstick
  taken=false;
 }
 
 synchronized void get() throws InterruptedException {  
// try to take this chopstick
 
   while (taken) // currently in use? So wait&
   Thread.currentThread().sleep(100);  
 
taken=true;
 }
}
 
public class DiningPhilosophers {
 
 public static void main(String[] args) {
   
  int numberOfPhilosophers = 5;
   
  Philosopher phil[] = new Philosopher[numberOfPhilosophers];
  Chopstick stick[] = new Chopstick[numberOfPhilosophers];
   
  for (int i =0; i<numberOfPhilosophers; ++i)
   stick[i] = new Chopstick();
   
  for (int i =0; i<numberOfPhilosophers; ++i){
 
      phil[i]=new Philosopher(i,  
stick[(i-1+numberOfPhilosophers)%numberOfPhilosophers],
stick[i]);
   
      phil[i].start();
   
  }
 }

perdoname_Asked:
Who is Participating?
 
digitalExpressConnect With a Mentor Commented:
http://www.cs.rit.edu/~ats/java-2000-1/html/skript-41.html

use this code

Philosopher  solves a deadlock problem : a philosopher reaches for a fork and tries to get the other one and eat, but puts down the first fork even if unsuccessful. One philosopher has his back to the table, i.e., he confuses the order of his forks to put some disorder into the system. Different amounts of output are produced on System.out  and System.err .
programs/thread/Philosopher.java
 
// Philosopher
import java.util.Random;
/** the Dining Philosophers.
 */
public class Philosopher extends Thread {
 /** the fork between two philosophers.
   */
 protected static class Fork {
   protected int me;				// number for trace
   protected boolean inUse;				// true if fork is in use
   public Fork (int me) {
     this.me = me;
   }
   /** returns true if fork is obtained, false if not.
     */
    public synchronized boolean get (int who) {
     System.err.println(who+(inUse ? " misses " : " grabs ")+me);
     return inUse ? false : (inUse = true);
   }
    /** drops the fork.
     */
   public synchronized void put (int who) {
     System.err.println(who+" drops "+me);
     inUse = false; notify();
   }
   /** returns once fork is obtained.
     */
   public synchronized void waitFor (int who) {
     while (! get(who))
	try {
	  wait();
       } catch (InterruptedException e) { e.printStackTrace(); }
   }
 }
 
 
 
The new idea is conditional access to the fork which otherwise acts as a semaphore.
 
The forks are distributed in main():
 
programs/thread/Philosopher.java
 
  /** make one diner.
   */
 public Philosopher (int me, Fork left, Fork right) {
   this.me = me; this.left = left; this.right = right;
 }
 protected static Random random = new Random();						// randomize
 protected int me;				// number for trace
 protected Fork left, right;				// my forks
 /** philosopher's body: think and eat 5 times.
   */
 public void run () {
   for (int n = 1; n <= 5; ++ n) {
     System.out.println(me+" thinks");
     try {
	Thread.sleep((long)(random.nextFloat()*1000));
     } catch (InterruptedException e) { e.printStackTrace(); }
     for (;;)
	try {
	  left.waitFor(me);
	  if (right.get(me)) {
	    System.out.println(me+" eats");
	    try {
	      Thread.sleep((long)(random.nextFloat()*1000));
	    } catch (InterruptedException e) { e.printStackTrace(); }
	    right.put(me);
	    break;
	  }
	} finally {
	  left.put(me);
	  Thread.yield();			// necessary to reschedule sparc ultra...
	}
   }
   System.out.println(me+" leaves");
 }
 /** sets up for 5 philosophers.
   */
 public static void main (String args []) {
   Fork f[] = new Fork[5];
   for (int n = 0; n < 5; ++ n) f[n] = new Fork(n);
   Philosopher p[] = new Philosopher[5];
   p[0] = new Philosopher(0, f[4], f[0]);						// backwards
   for (int n = 1; n < 5; ++ n) p[n] = new Philosopher(n, f[n-1], f[n]);
   for (int n = 0; n < 5; ++ n) p[n].start();
 }
}
 

Open in new window

0
 
contactkarthiCommented:
do you want to start fixing the code or use the code provided in the above link?
0
 
perdoname_Author Commented:
@ digitalExpress:
Thanks for the code !

But i prefer to start fixing my code cuz its a special scenario that all four philosophers shall die after having eaten 500 times.

Thanks in advance!
0
 
contactkarthiCommented:
in middle of a work here...
 give me some time will start pointing out
0
 
perdoname_Author Commented:
okay thanks !!!
0
All Courses

From novice to tech pro — start learning today.