|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
TheRingMasterclass provides two methods that the
GamesThreaduses to coordinate its activities with the other threads in the Game application:waitForFirstPlayerandwaitForGameToEnd. As the names of these methods imply, these methods causeGamesThreadto wait until for a condition set by another thread.
Therunmethod forGamesThreadcontains a loop that continues until told to stop by theControlPane. Each iteration through the loop is a single BINGO game. At the top of the loop, and therefore at the beginning of each BINGO game, therunmethod forGamesThreadcallsRingMaster'swaitForFirstPlayermethod which looks like this:In plain English, this method causes thesynchronized void waitForFirstPlayer() { gameNumber++; state = WAITING; socketGate.sendGameStatusMessage(statusString()); while (state == WAITING) { try { wait(); } catch (InterruptedException e) { } } socketGate.sendGameStatusMessage("Beginning count down ... "); }GamesThreadto wait until the first Player registers. Let's look at the code to find out how.First the method sets the
RingMaster'sstatetoWAITING. Then,waitForFirstPlayerenters a loop that causes the current thread towaitas long asstateis stillWAITING. Which begs the question "How can this loop ever end?" The answer is that another thread, a thread in the Player application changesstateand wakes up theGamesThread. Here's how.To register, a Player application makes a remote method call to
RegistrarImpl'smayIPlaymethod. Part of the registration process involves adding aPlayerRecordto theRosterwhich, if this is the first player to register, also involves calling theRingMaster'sstartCountDownmethod.startCountDownlooks like this:This method changes thesynchronized void startCountDown() { state = COUNTINGDOWN; notifyAll(); }statetoCOUNTINGDOWNand then callsnotifyAll.notifyAllcausesGamesThreadto return from itswaitmethod. Remember that the call towaitis in a loop that continues as long asstateisWAITING. So afterGamesThreadreturns fromwait, it checks thestate, which is no longerWAITINGand so exits the loop. So this method causesGamesThreadto return fromwaitForFirstPlayer.Thus when
GamesThreadcallswaitForFirstPlayerthe effect is that the threads stops and waits until the first player registers for the game. Then the count down begins.[PENDING: consider diagraming the flow of this using diagrams similar to Doug Lea's]
GamesThreaduses a very similar mechanism to wait until the current game is over. A game ends when a Player wins or theBallAnnouncerruns out of balls). The code is very similar to that used byGamesThreadto wait for the first player to register so we'll briefly point out the classes and methods involved and let you figure out the rest.After
GamesThreadhas created aBallAnnouncerand started it,GamesThreadcallsRingMaster'swaitForGameToEndmethod which looks like this:Like thesynchronized void waitForGameToEnd() { while (gameInProgress()) { try { wait(); } catch (InterruptedException e) { } } }waitForFirstPlayermethod, this method waits until thestatechanges and indicates that the game is no longer in progress. This causes theGamesThreadto go into a wait state and do nothing.The
stategets changed whenRingMaster'ssetGameOvermethod gets called.synchronized void setGameOver() { state = GAMEOVER; announceBall(new BingoBall(BingoBall.GAME_OVER)); announcedBalls.removeAllElements(); announcedBalls.push(new BingoBall(BingoBall.FREE_SPACE)); roster.removeAllElements(); notifyAll(); }setGameOvercan be called under two conditions:
- A Player won the game. When a Player wins, it remotely calls
RegistrarImpl'sBINGOmethod which verifies the claim. If the BINGO claim is valid, thenBINGOcallssetGameOver.- The
BallAnnouncerannounced all of the balls and there was no winning claim from any Player. In this case theBallAnnouncercallssetGameOver.setGameOverchanges theRingMaster'sstateand then callsnotifyAllwhich wakes upGamesThread.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.