943,663 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 19842
  • Java RSS
Jul 10th, 2007
0

Dining Philosophers Problem

Expand Post »
can any one explain about the below code which is Dining Philosophers Problem..
Java Syntax (Toggle Plain Text)
  1. //Dining Philosophers Problem
  2. import java.util.Random;
  3. class Monitor {
  4. int phil_States[] = new int[5]; // 0=not_waiting, 1=waiting // 2=eating
  5. boolean fork_States[] = new boolean[5]; // false = in use, true = free
  6. Monitor() { // constructor
  7. for(int i=0;i<5;i++) {
  8. phil_States[i]=0;
  9. fork_States[i]=true;
  10. }
  11. }
  12. synchronized void print_State()
  13. {
  14. System.out.println(); // newline
  15. for(int i=0;i<5;i++)
  16. System.out.print(" " + phil_States[i]);
  17. }
  18. synchronized void ask_to_eat(int pId)
  19. {
  20. while(!fork_States[pId] || !fork_States[(pId+1)%4])
  21. { // while it can't have both forks, wait
  22. phil_States[pId] = 1;
  23. try
  24. {wait();}
  25. catch(InterruptedException e){} // it gets released
  26. } // by a process doing a call to notify()
  27. phil_States[pId] = 2; // eating
  28. fork_States[pId] = false; // in use
  29. fork_States[(pId+1)%4] = false;
  30. }
  31. synchronized void ask_to_leave(int pId)
  32. {
  33. fork_States[pId] = true; // available
  34. fork_States[(pId+1)%4] = true;
  35. phil_States[pId] = 0; // thinking
  36. notify(); // free the Phil that has waited the longest
  37. }
  38. }
  39.  
  40. class Diners {
  41. public static void main(String args[]) { // execution of the whole
  42. Monitor m = new Monitor(); // thing begins here
  43. Timer t = new Timer(m); // make a new timer
  44. Phil p[] = new Phil[5]; // make an array of 5 refs to Phils
  45. for(int i=0; i<5; i++)
  46. p[i] = new Phil(i,m,t); // create the phils and start them
  47. }
  48. }
  49. class Phil implements Runnable {
  50. Monitor m;
  51. Timer t;
  52. Random r = new Random(); // Random number generator object
  53. int pId;
  54. float time;
  55. Phil(int pId, Monitor m, Timer t) { // constructor
  56. System.out.println(pId + "is started: ");
  57. this.pId = pId;
  58. this.m = m;
  59. this.t = t;
  60. new Thread(this, "Phil").start(); // make a new thread and start it
  61. }
  62. public void run() { // must override run, this is what
  63. for(int i=0; i<20; i++) { // is executed when the thread starts
  64. m.ask_to_eat(pId); // running
  65. time = 1000*r.nextFloat();
  66. try {Thread.sleep((int)time);} catch(Exception e){}
  67. m.ask_to_leave(pId);
  68. time = 1000*r.nextFloat();
  69. try {Thread.sleep((int)time);} catch(Exception e){}
  70. }
  71. t.report_Stop(); // tell the timer this one is done
  72. }
  73. }
  74. class Timer implements Runnable {
  75. Monitor m;
  76. int completed;
  77. Timer(Monitor m) { // constructor
  78. this.m = m;
  79. new Thread(this, "Tim").start(); // make a new thread and start it
  80. completed=0;
  81. }
  82. public void report_Stop() {
  83. completed++;
  84. }
  85. public void run() { // must override run(), this is
  86. while(completed!=5) { // what happens when the thread
  87. m.print_State(); // begins
  88. try {Thread.sleep(500);} catch(Exception e){}
  89. }
  90. }
  91. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
paeez is offline Offline
11 posts
since Feb 2007
Jul 10th, 2007
0

Re: Dining Philosophers Problem

Explain what about it? You haven't asked a question.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Jul 11th, 2007
0

Re: Dining Philosophers Problem

explain what it does..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
paeez is offline Offline
11 posts
since Feb 2007
Jul 11th, 2007
0

Re: Dining Philosophers Problem

Looks like a threaded program. What d you know about threads so far?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jul 11th, 2007
0

Re: Dining Philosophers Problem

Click to Expand / Collapse  Quote originally posted by paeez ...
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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Jul 11th, 2007
0

Re: Dining Philosophers Problem

seeing as the solution to the problem is contained in the official Sun tutorial, I wonder what the problem is
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jul 13th, 2007
0

Re: Dining Philosophers Problem

Reputation Points: 21
Solved Threads: 7
Junior Poster in Training
ProgrammersTalk is offline Offline
83 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: classes for an online share trading system
Next Thread in Java Forum Timeline: fail to load Main-class manifest attribute from ......problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC