| | |
Dining Philosophers Problem
![]() |
•
•
Join Date: Feb 2007
Posts: 7
Reputation:
Solved Threads: 0
can any one explain about the below code which is Dining Philosophers Problem..
Java Syntax (Toggle Plain Text)
//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){} } } }
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.
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.
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
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
The ProgrammersTalk Community | Programming & Marketing | Buying & Selling Script
Hang out place of novice and intermediate programmers
Hang out place of novice and intermediate programmers
![]() |
Other Threads in the Java Forum
- Previous Thread: classes for an online share trading system
- Next Thread: fail to load Main-class manifest attribute from ......problem
| Thread Tools | Search this Thread |
2dgraphics 3d @param affinetransform android api apple applet application arc arguments array arrays automation banking binary binarytree bluetooth byte chat chatprogramusingobjects class client code color compare component count database derby design detection eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image integer interface j2me java java.xls javadesktopapplications javaprojects jni jpanel julia keytool keyword linux list macintosh map method methods mobile netbeans newbie object os pong problem producer program programming project projectideas read recursion reference replaysolutions rim scanner server set size sms sort sql string swing terminal threads transforms tree ui unicode web windows






