// Philosopher thread in Java // George F. Riley, ECE3055, Spring 2003 class Philosopher extends Thread { private int myId; private Object cond; private Object mutex; static volatile private int doneCount = 0; Philosopher(int id, Object c, Object m) { myId = id; // Set thread identifier cond = c; // Save condition variable to use mutex = m; // Save mutex object } public void run() { System.out.println("Hello from philosopher " + myId); yield(); synchronized(mutex) { doneCount++; System.out.println("Philosopher " + myId + " doneCount " + doneCount); if (doneCount == 5) { synchronized(cond) { cond.notify(); // Notify main thread all done } } } System.out.println("Philosopher " + myId + " exiting"); } }