Java Reference
In-Depth Information
direction
#
1.0;
eventName
#
"UP";
synchronized
(
this
) { notify(); }
}
// lowers the bar and notifies the "DOWN" event
public void
lower() {
if
(angle
##
0.0)
return
;
direction
#
-1.0;
eventName
#
"DOWN";
synchronized
(
this
) { notify(); }
}
public void
run() {
while
(alive)
try
{
synchronized
(
this
) { wait(); }
// repaints the stopping bar every 9 degrees
for
(
int
i
#
0; i<10; i
!!
) {
angle
!#
direction * Math.PI / 20.0;
this
.repaint();
thread.sleep(200);
}
this
.notifyObservers(eventName);
}
catch
(InterruptedException ie)
{ ie.printStackTrace(); }
}
}
Class
Controller
has a reference to all the physical devices of the access
control system. It listens to the events raised by the photocells, the ticket
dispenser and the stopping bar. Method
run()
implements the finite state
machine depicted in Figure 11.5.
package
parking;
public class
Controller
extends
EventProcess {
TrafficLight trafficLight;
// a reference to the traffic light
PhotoCell inPhotoCell;
// a reference to the first photocell
PhotoCell outPhotoCell;
// a reference to the second photocell
StoppingBar stoppingBar;
// a reference to the stopping bar
TicketDispenser ticketDispenser;
// a reference to the ticket dispenser
int
phase
#
1; // the counter of the current phase
// the constructor