1 20 class Fork { 21 } 22 23 class Philosopher extends Thread { 24 Fork left; 25 Fork right; 26 27 public Philosopher(Fork left, Fork right) { 28 this.left = left; 29 this.right = right; 30 start(); 31 } 32 33 public void run() { 34 synchronized (left) { 36 synchronized (right) { 37 } 39 } 40 } 41 } 42 43 class DiningPhil { 44 static final int N = 2; 45 public static void main(String [] args) { 46 Fork[] forks = new Fork[N]; 48 for (int i = 0; i < N; i++) { 49 forks[i] = new Fork(); 50 } 51 for (int i = 0; i < N; i++) { 52 new Philosopher(forks[i], forks[(i + 1) % N]); 53 } 54 } 56 } 57 58 | Popular Tags |