The Java TM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search

Trail: Putting It All Together
Lesson: BINGO!

## Synchronizing and Coordinating Threads

> 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 `Main` thread. The Java runtime system automatically creates this thread. The `main` method for the Player application is implemented in the `Player` class and runs in the `Main` thread. The `main` 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` and `GameListenerThread`. 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 program uses these threads:
> - The Game application has a `Main` thread just like the Player application. The `main` method for the Game application is implemented in the `BINGO` class and runs in the `Main` thread. As with the `Main` 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 the `ControlPane` 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`, and `GameListenerThread`. 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 the `Roster` 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 through `synchronized` blocks and `synchronized` 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, the `GamesThread` 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 using `wait` and `notifyAll`.

| Start of Tutorial > Start of Trail > Start of Lesson | Search