1 5 6 9 10 package DiningPhilosophers; 11 12 public class Fork { 13 private boolean free; 14 private int num; 15 16 Fork(int n) { 17 free = true; 18 this.num = n; 19 } 20 21 synchronized void grab(int phil) { 22 while (!free) { 23 try { 24 wait(); 25 } catch (InterruptedException e) { 26 System.out.println(e); 27 } 28 } 29 free = false; 30 System.out.println(phil+" Grabbed fork#: "+num); 31 } 32 33 synchronized void release(int phil) { 34 free = true; 35 notify(); 36 System.out.println(phil+" Released fork#: "+num); 37 } 38 } 39 | Popular Tags |