RSS Forums RSS

Dining Philosophers Problem

Please support our Java advertiser: Programming Forums
Reply
Posts: 7
Reputation: paeez is an unknown quantity at this point 
Solved Threads: 0
paeez paeez is offline Offline
Newbie Poster

Question Dining Philosophers Problem

  #1  
Jul 10th, 2007
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){}
   }
}
}
AddThis Social Bookmark Button
Reply With Quote  
Posts: 4,111
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 458
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Dining Philosophers Problem

  #2  
Jul 10th, 2007
Explain what about it? You haven't asked a question.
Reply With Quote  
Posts: 7
Reputation: paeez is an unknown quantity at this point 
Solved Threads: 0
paeez paeez is offline Offline
Newbie Poster

Re: Dining Philosophers Problem

  #3  
Jul 11th, 2007
explain what it does..
Reply With Quote  
Posts: 5,068
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 355
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Dining Philosophers Problem

  #4  
Jul 11th, 2007
Looks like a threaded program. What d you know about threads so far?
*Voted best profile in the world*
Reply With Quote  
Posts: 4,111
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 458
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Dining Philosophers Problem

  #5  
Jul 11th, 2007
Originally Posted by paeez View Post
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_...ophers_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.
Reply With Quote  
Posts: 5,749
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Solved Threads: 205
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Dining Philosophers Problem

  #6  
Jul 11th, 2007
seeing as the solution to the problem is contained in the official Sun tutorial, I wonder what the problem is
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote  
Posts: 83
Reputation: ProgrammersTalk is an unknown quantity at this point 
Solved Threads: 7
ProgrammersTalk's Avatar
ProgrammersTalk ProgrammersTalk is offline Offline
Junior Poster in Training

Re: Dining Philosophers Problem

  #7  
Jul 13th, 2007
The ProgrammersTalk Community | Programming & Marketing | Buying & Selling Script
Hang out place of novice and intermediate programmers
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 6761 | Replies: 6 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:23 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC