Putting It All Together |
The following a diagram shows the threads in both the Game and Player applications.[PENDING: figure here]
As you can see from the diagram, the Player program uses these threads:
The Game program uses these threads:
- The
Main
thread. The Java runtime system automatically creates this thread. Themain
method for the Player application is implemented in thePlayer
class and runs in theMain
thread. Themain
method creates all of the objects necessary for the program to run, initializes them, and starts whatever other threads are necessary at this juncture. This thread does not need to be synchronized or coordinated with any other threads: it does its job and then stops.- The Player has two listener threads that listen for status information broadcast by the Game:
BallListenerThread
andGameListenerThread
. These threads run independently of all other threads and their activities need not be synchronized or coordinated with any others.- The AWT thread is created automatically by the JDK runtime. The drawing and event handling activities of the AWT thread must be coordinated with the activities of other threads in the Player application. [PENDING: explain why here or somewhere else?]
- The Game application has a
Main
thread just like the Player application. Themain
method for the Game application is implemented in theBINGO
class and runs in theMain
thread. As with theMain
thread for the Player application, this thread does not need to be synchronized or coordinated with any other threads: it does its job and then stops.- The
GamesThread
is created by theControlPane
when the user pushes the Let the Games Begin button. This thread runs a continuous battery of BINGO games, one after the other. Each game begins with a waiting period, then a registration period, then the game. The activities of this thread must be coordinated with the activities of other threads in the program.- For each game,
GamesThread
creates another thread,BallAnnouncer
, that announces the balls for the current game. The activities of this thread must be coordinated with the activities of other threads in the program.- The Game program also has three threads, one for each of its status panes:
BallListenerThread
,PlayerListenerThread
, andGameListenerThread
. These threads listen for information broadcast from the game and update their status panes with that information. These threads run independently of all other threads and their activities need not be synchronized or coordinated with any other threads in the game.- The AWT thread is created automatically by the JDK runtime and doesn't affect thread synchronization in the Game application.
Using Synchronized Code Segments
As the diagram shows, different threads access the same objects or data. For example, when two Players attempt to register simultaneously, the two Player threads (running in different VMs) both modify theRoster
object in the Game application by adding a player to the roster. In these cases, the threads must be synchronized so that the data accesses are guaranteed to be thread-safe. This is achieved throughsynchronized
blocks andsynchronized
methods in the Game application.Synchronizing Threads in the Player
The Player application uses a customized EventQueue that handles thread synchronization automatically.Coordinating Threads in the Game
In the Game application, some threads must coordinate their activities. For example, theGamesThread
must wait until the current game is over (a condition that is set by another thread) before it can begin another game. In such a case, the threads coordinate with one another usingwait
andnotifyAll
.
Putting It All Together |