I am working on this project in my java text book and I am unable to figure out how the multi thread part of the program works. The program that I am working on is a traffic light simulation program. I have a little understanding how multi thread works. I am particularly baffled about the waitForChange() method. I just cant figure out how it knows that the simulated light has changed. I understand that the method is syncronized so, when the wait() method is called, it waits until it is notified to start again. I understand how it works a little bit but, not enough to do the exercise in my text book. could someone please help me?
Thanks,
Truck35
// An improved version of the traffic light simulation that // stores the light delay in TrafficLightColor. // An enumeration of the colors of a traffic light. enum TrafficLightColor { RED(12000), GREEN(10000), YELLOW(2000); private int delay; TrafficLightColor(int d) { delay = d; } int getDelay() { return delay; } } // A computerized traffic light. class TrafficLightSimulator implements Runnable { private Thread thrd; // holds the thread that runs the simulation private TrafficLightColor tlc; // holds the current traffic light color boolean stop = false; // set to true to stop the simulation boolean changed = false; // true when the light has changed TrafficLightSimulator(TrafficLightColor init) { tlc = init; thrd = new Thread(this); thrd.start(); } TrafficLightSimulator() { tlc = TrafficLightColor.RED; thrd = new Thread(this); thrd.start(); } // Start up the light. public void run() { while(!stop) { // Notice how this code has been simplified! try { Thread.sleep(tlc.getDelay()); } catch(InterruptedException exc) { System.out.println(exc); } changeColor(); } } // Change color. synchronized void changeColor() { switch(tlc) { case RED: tlc = TrafficLightColor.GREEN; break; case YELLOW: tlc = TrafficLightColor.RED; break; case GREEN: tlc = TrafficLightColor.YELLOW; } changed = true; notify(); // signal that the light has changed } // Wait until a light change occurs. synchronized void waitForChange() { try { while(!changed) wait(); // wait for light to change changed = false; } catch(InterruptedException exc) { System.out.println(exc); } } // Return current color. synchronized TrafficLightColor getColor() { return tlc; } // Stop the traffic light. synchronized void cancel() { stop = true; } } class TrafficLightDemo { public static void main(String args[]) { TrafficLightSimulator tl = new TrafficLightSimulator(TrafficLightColor.GREEN); for(int i=0; i < 9; i++) { System.out.println(tl.getColor()); tl.waitForChange(); } tl.cancel(); } }