954,136 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Dining Philosophers Problem

can any one explain about the below code which is Dining Philosophers Problem..

//Dining Philosophers Problem
import java.util.Random;
class Monitor {
int phil_States[] = new int[5];   // 0=not_waiting, 1=waiting // 2=eating
boolean fork_States[] = new boolean[5];  // false = in use, true = free
Monitor() {        // constructor
   for(int i=0;i<5;i++) {
      phil_States[i]=0;
      fork_States[i]=true;
   }
}
synchronized void print_State()
{
System.out.println();        // newline
for(int i=0;i<5;i++)
     System.out.print(" " + phil_States[i]);        
}
synchronized void ask_to_eat(int pId)
{
   while(!fork_States[pId] || !fork_States[(pId+1)%4])
   {    // while it can't have both forks, wait
      phil_States[pId] = 1;  
      try 
  {wait();} 
  catch(InterruptedException e){} // it gets released
   }    // by a process doing a call to notify()
   phil_States[pId] = 2;  // eating
   fork_States[pId] = false;  // in use
   fork_States[(pId+1)%4] = false;
}
synchronized void ask_to_leave(int pId)
{
     fork_States[pId] = true; // available
     fork_States[(pId+1)%4] = true;
     phil_States[pId] = 0;  // thinking
     notify(); // free the Phil that has waited the longest
}
}
 
class Diners {
public static void main(String args[]) { // execution of the whole
   Monitor m = new Monitor(); // thing begins here
   Timer t = new Timer(m);  // make a new timer
   Phil p[] = new Phil[5];  // make an array of 5 refs to Phils
   for(int i=0; i<5; i++)
      p[i] = new Phil(i,m,t); // create the phils and start them
}
}
class Phil implements Runnable {
Monitor m;
Timer t;
Random r = new Random();  // Random number generator object
int pId;
float time;
Phil(int pId, Monitor m, Timer t) { // constructor
   System.out.println(pId + "is started: ");
   this.pId = pId;
   this.m = m;
   this.t = t;
   new Thread(this, "Phil").start(); // make a new thread and start it
}
public void run() {   // must override run, this is what
   for(int i=0; i<20; i++) {  // is executed when the thread starts
     m.ask_to_eat(pId);  // running
     time = 1000*r.nextFloat();
     try {Thread.sleep((int)time);} catch(Exception e){}
     m.ask_to_leave(pId);
     time = 1000*r.nextFloat();
     try {Thread.sleep((int)time);} catch(Exception e){}
   }
t.report_Stop();   // tell the timer this one is done
}       
}
class Timer implements Runnable {
Monitor m;
int completed;
Timer(Monitor m) {   // constructor
   this.m = m;
   new Thread(this, "Tim").start(); // make a new thread and start it
   completed=0;
}
public void report_Stop() {
   completed++;
}
public void run() {   // must override run(), this is 
   while(completed!=5) {  // what happens when the thread
      m.print_State();  // begins
      try {Thread.sleep(500);} catch(Exception e){}
   }
}
}
paeez
Newbie Poster
11 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

Explain what about it? You haven't asked a question.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

explain what it does..

paeez
Newbie Poster
11 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

Looks like a threaded program. What d you know about threads so far?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
explain what it does..


Well, I would say it is a possible solution to the Dining Philosophers problem, which one quick Google search provided the explanation: http://en.wikipedia.org/wiki/Dining_philosophers_problem

Are you wanting someone to walk you through how the code works step by step? I'm still not understanding what your question is exactly.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

seeing as the solution to the problem is contained in the official Sun tutorial, I wonder what the problem is :)

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
ProgrammersTalk
Junior Poster in Training
84 posts since Jun 2007
Reputation Points: 21
Solved Threads: 7
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You