View Single Post
Join Date: Feb 2007
Posts: 7
Reputation: paeez is an unknown quantity at this point 
Solved Threads: 0
paeez paeez is offline Offline
Newbie Poster

Dining Philosophers Problem

 
0
  #1
Jul 10th, 2007
can any one explain about the below code which is Dining Philosophers Problem..
  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. }
Reply With Quote