Sunday 15 July 2012

java - Starvation occuring in the Dining Philosophers code -


Text after "

Here is the philosophical dinner consent implementation of your problem. I work only for two yarn and the rest is really hungry;) I appreciate if you can get the reason for it.

Here is the production:.

  philosopher 2 philosopher eating 0 philosophically eating 2 philosophical eating 0 philosophical eating 2 philosophical eating 0 philosophical eating 2 philosophical eating 0 philosophical 2 eating The philosopher is eating 2 the philosophical is eating 0 the philosopher is eating 2 the philosopher is eating ate eating   

Here is the code:

  Public class PhilosophersDinner {public static void (String [] args) {Chopstick2 [] chopsticks = new Chopstick2 [5]; Philosopher [] philosophers = new philosophers [5]; For (Int i = 0; I <5; i ++) {Chopstick 2 Stick = New Chopstick 2 (); Chopsticks [i] = stick; } (Int i = 0; I & lt; 5; i ++) {philosophical philo = new philosopher (chopsticks), chopsticks [(i +1)% 5], I + ""); Philosopher [i] = Philo; } For (int i = 0; i & lt; philosophers.length; i ++) {new thread (philosophers [i]) start () .; }}} Class philosopher applies Runnabel {Private Final Chopstick 2 left; Private Finals Chopstick 2 Right; Private last string id; Public philosopher (Chopstick 2L, Chopstick 2R, String ID) {this.left = l; this. Right = r; This.id = ID; } Public Zero Earns (if (left.pickUp ()) {if (right.pickUp ()) {chew (); Deleting ();} and left.putdown ();}} Public Zero Run () (Int i = 0; i & lt; 10; i ++) {eat ();}} public zeros chub () {try {system.out.println ("philosopher" + id + "eating" ); Thread.Sleep (400);} Hold (Interrupted E) e.printStackTrace (); // Release Lock Here?}} Public Zero PutDown () {left.putDown (); right.putDown ();} } Square Chopstick 2 {Private volatile bullion taken = false; Public synchronized boolean pickup () {If (! ); {True = back; true;} and false return;} Public synchronized script () {taken = wrong;}}    

The problem is that the first two philosophers to spend chocastics in half of the second meal and during that time to eat the other three dinners Try and leave.

For a solution, all the philosophers have a specific number to eat a certain amount. Instead of Ane. (long time I = System.currentTimeMillis () + 10000; i> System. CurrentTimeMillis ();) (Eat); }

No comments:

Post a Comment