/* DiningPhilosophers.java - the classic problem Written by Robert Sheehan 01/09/97 */ import java.awt.*; public class DiningPhilosophers extends Frame { public final static int NUMBER = 5; private ThreadGroup allPhilosophers; public DiningPhilosophers() { ForkControl controller; Philosopher philosopher; SafeTextArea philosopherText; Panel textPanel; Panel buttonPanel; Button goButton; Button stopButton; resize(800, 400); // set the Frame size setTitle("Dining Philosophers"); // set the title setLayout(new FlowLayout()); // method of laying out Frame contents textPanel = new Panel(); add("Center", textPanel); buttonPanel = new Panel(); goButton = new Button("Go"); stopButton = new Button ("Stop"); buttonPanel.add(goButton); buttonPanel.add(stopButton); add("South", buttonPanel); controller = new ForkControl(); allPhilosophers = new ThreadGroup("Philosophers"); for (int i = 0; i < NUMBER; i++) { philosopherText = new SafeTextArea(21, 15); textPanel.add(philosopherText); philosopher = new Philosopher(allPhilosophers, String.valueOf(i), philosopherText, controller); philosopherText.setText("Philosopher " + philosopher.getName()); philosopher.start(); } } // JDK 1.0.2 event handling at the moment public boolean action(Event event, Object what) { if (what.equals("Go")) allPhilosophers.resume(); if (what.equals("Stop")) allPhilosophers.suspend(); return true; } public boolean handleEvent(Event event) { if (event.id == Event.WINDOW_DESTROY) { hide(); dispose(); System.exit(0); return true; } return super.handleEvent(event); } // the program starts here public static void main(String[] args) { (new DiningPhilosophers()).show(); // construct the Frame and show it } }