|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The following 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
Mainthread. The Java runtime system automatically creates this thread. Themainmethod for the Player application is implemented in thePlayerclass and runs in the
Mainthread. Themainmethod 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:
BallListenerThreadandGameListenerThread. 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
Mainthread just like the Player application. Themainmethod for the Game application is implemented in theBINGOclass and runs in the
Mainthread. As with theMainthread 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
GamesThreadis created by theControlPanewhen 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,
GamesThreadcreates 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.
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 theRosterobject 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 throughsynchronizedblocks andsynchronizedmethods in the Game application.
The Player application uses a customized EventQueue that handles thread synchronization automatically.
In the Game application, some threads must coordinate their activities. For example, theGamesThreadmust 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 usingwaitandnotifyAll.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.