File SystemClock.java simulates a clock. At the end of the line public class SystemClock... appears the phrase implements Runnable. This is Java's way of indicating that this class has a method run(), which should be called by the system to execute a class instance as a thread. We'll look at run later.
So, instances of this class are separate threads of execution.
The class has two variables that deal with the clock itself: counter contains the number of clock ticks that have happened thus far; period is the number of milliseconds to wait between clock ticks.
Four fields are components that go in the Frame (which you see on the screen): two Labels, a Textfield, and a Button.
Look at the constructors for SystemClock (including method initialize). In the game Checkers, we used layout manager GridBagLayout to place components in the Frame. Here, because our Frame layout is so much simpler, we can use the default layout manager for Frames, BorderLayout. This layout manager allows us to place components in five places: North, West, Center, East, and South:
North | ||
West | Center | East |
South |
In method initialize, you can see the four calls on method add, which place the four components in the Frame.
Two methods in SystemClock are fairly obvious: reset sets the clock back to zero, and ticks is used by other processors to get the clock's value.
Method run is of special significance. The main program (method main of CruiseControl), calls cThread.start(), where cThread is the variable that contains the instance of SystemClock. In turn, method start will call run to start execution of this thread.
Method run is basically a loop in which each iteration does the following:
The code to notify the other processes should be performed only when no other process is referencing variables associated with this thread. This ``lock out'' is indicated by the phrase synchronize. We don't go into any more detail here on this notion of synchronization.
Finally, take a look at method action, which is called when a button is pressed. There is only one button in the Frame, so there is no need for code to determine what the action was. The pressing of the button is processed by (0) reading TextField periodField, (1) trimming whitespace from both ends of it, (2) converting this String value to an integer, and (3) finally storing the integer in period (if the value is at least 100).