/* Philosopher.java - one of the dining philosophers Written by Robert Sheehan 01/09/97 */ public class Philosopher extends Thread{ private SafeTextArea output; private ForkControl controller; private int name; public Philosopher(ThreadGroup group, String name, SafeTextArea output, ForkControl controller) { super(group, name); this.name = Integer.parseInt(name); this.output = output; this.controller = controller; } public void run() { while (true) { think(); eat(); } } private void think() { output.appendText("\ngoing to sleep"); try { sleep((int)(Math.random() * 1000 + 1000)); } catch (InterruptedException e) { output.appendText("\ninterrupted while thinking"); } } private void eat() { try { output.appendText("\ngetting forks"); controller.getBothForks(name); output.appendText("\neating"); sleep((int)(Math.random() * 1000 + 1000)); output.appendText("\nreturning forks"); controller.putBackBothForks(name); } catch (InterruptedException e) { output.appendText("\ninterrupted while eating"); } } }