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){}
}
}
}``````

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

explain what it does..

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

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.

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

Can someone tell please, this solution prevents a problem of starvation?

commented: zombie homework kiddo -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.