DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Dining Philosophers Problem (http://www.daniweb.com/forums/thread83206.html)

paeez Jul 10th, 2007 5:04 pm
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){}
  }
}
}

Ezzaral Jul 10th, 2007 6:15 pm
Re: Dining Philosophers Problem
 
Explain what about it? You haven't asked a question.

paeez Jul 11th, 2007 3:41 am
Re: Dining Philosophers Problem
 
explain what it does..

iamthwee Jul 11th, 2007 7:31 am
Re: Dining Philosophers Problem
 
Looks like a threaded program. What d you know about threads so far?

Ezzaral Jul 11th, 2007 12:41 pm
Re: Dining Philosophers Problem
 
Quote:

Originally Posted by paeez (Post 402029)
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.

jwenting Jul 11th, 2007 1:16 pm
Re: Dining Philosophers Problem
 
seeing as the solution to the problem is contained in the official Sun tutorial, I wonder what the problem is :)

ProgrammersTalk Jul 13th, 2007 7:35 pm
Re: Dining Philosophers Problem
 
Check these tutorials, hopefully it will help you out
http://www.programmerstalk.net/thread845.html
http://www.cs.mtu.edu/~shene/NSF-3/e...-philos-1.html
http://www.cs.utk.edu/~plank/plank/c...l/lecture.html

Hopefully it would be helpful for you :)


All times are GMT -4. The time now is 8:37 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC