// File: Race9.java (Module 12) // // Author: Rahul Simha // Created: Nov 17, 1998 // // Waiting and notification. import java.awt.*; import java.awt.event.*; class NewFrame extends Frame { Canvas c; public NewFrame () { // Frame properties. this.setTitle ("Dog Race"); this.setBackground (Color.cyan); this.setResizable (true); this.setSize (600,200); // Quit button. Panel p = new Panel (); Button quitb = new Button ("QUIT"); quitb.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent a) { System.exit (0); } } ); p.add (quitb); // Pressing "start" calls race() Button startb = new Button ("START"); startb.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent a) { race (); } } ); p.add (startb); this.add (p, BorderLayout.SOUTH); // A canvas to draw the results. c = new Canvas(); c.setBackground (Color.white); this.add (c, BorderLayout.CENTER); this.setVisible (true); } void race () { Dimension D = c.getSize (); // Finish-line is at the right end of the canvas. int finish_line = D.width; // Create the dog monitor. DogMonitor monitor = new DogMonitor (2); // Create two dog instances with different ID's. Dog d1 = new Dog (1, c, monitor, 2); Dog d2 = new Dog (2, c, monitor, 1); // Create a Thread instance for each dog. // Note: the class Dog must implement the // Runnable interface. Thread d1_thread = new Thread (d1); Thread d2_thread = new Thread (d2); // Start running the threads. // ("start" is a method in Thread). d1_thread.start(); d2_thread.start(); } } // A monitor class with synchronized access. class DogMonitor { // The position of each dog. int[] position; public DogMonitor (int num_dogs) { position = new int [num_dogs+1]; for (int i=1; i<=num_dogs; i++) position[i] = 20; } // Set the new position of Dog# ID. public synchronized void set_position (int ID, int new_position) { position[ID] = new_position; } // Get the current position of Dog# ID. public synchronized int get_position (int ID) { return position[ID]; } // A waiting dog needs to call a synchronized // wait method inside the monitor. public synchronized void synch_wait () { try { // wait() is inherited from Object. wait (); } catch (InterruptedException e) { System.out.println (e); } } // Tell the other dog that it can continue. public synchronized void synch_notify () { // notify() is inherited from Object. notify (); } } class Dog implements Runnable { int ID; // An ID. Canvas c; // The canvas on which to draw. DogMonitor monitor; // Store a reference to the monitor. int OtherID; // ID of the other dog. public Dog (int ID, Canvas c, DogMonitor monitor, int OtherID) { this.ID = ID; this.c = c; this.monitor = monitor; this.OtherID = OtherID; // Draw ID on canvas. Graphics g = c.getGraphics (); g.drawString (""+ID, 5, 20*ID+8); } public void move () { // Move a random amount. int position = monitor.get_position (ID); int new_position = position + (int) UniformRandom.uniform (1,50); monitor.set_position (ID, new_position); // Draw new position. Graphics g = c.getGraphics (); int size = new_position - position; g.fillRect (position, 20*ID, size, 10); } public void run () { // Compute the finish line distance. int finish_line = c.getSize().width; // While not complete... while (monitor.get_position (ID) < finish_line) { try { Thread.sleep ((int)UniformRandom.uniform (300,600)); } catch (InterruptedException e) { System.out.println (e); } move (); // Check if you need to wait. int other = monitor.get_position (OtherID); int mine = monitor.get_position (ID); if (mine - other > 20) { System.out.println ("ID=" + ID + " waiting: mine=" + mine + ", other=" + other); monitor.synch_wait (); System.out.println ("ID=" + ID + " continuing"); } // See if the other guy is waiting for me. if (other - mine <= 20) { System.out.println ("ID=" + ID + " notifying: mine=" + mine + ", other=" + other); monitor.synch_notify(); } } // endwhile } } public class Race9 { public static void main (String[] argv) { NewFrame nf = new NewFrame (); } }