26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

880 Multithreading Chapter 15<br />

Method ride guarantees that only one Person may ride the Eleva<strong>to</strong>r at a time—<br />

as described in Section 15.5, only one synchronized method may be active on an object<br />

at once, so all other Person threads attempting <strong>to</strong> invoke ride must wait for the current<br />

thread <strong>to</strong> exit method ride.<br />

Method ride invokes static method sleep of class Thread <strong>to</strong> put the Person<br />

thread in<strong>to</strong> the sleep state, which represents the Person waiting for the ride <strong>to</strong> complete.<br />

We must specify the maximum amount of time that a Person will wait for the Eleva<strong>to</strong>r<br />

<strong>to</strong> complete traveling—however, there is no such information specified in the problem statement.<br />

We introduce a new attribute that represents this time—maxTravelTime, <strong>to</strong> which<br />

we arbitrarily assign a value of 10 minutes (i.e., a Person will wait 10 minutes for the<br />

travel <strong>to</strong> complete). Attribute maxTravelTime is a safeguard in case the Eleva<strong>to</strong>r—<br />

for whatever reason—never reaches the other Floor. The Person should never have <strong>to</strong><br />

wait this long—if the Person waits 10 minutes, then the Eleva<strong>to</strong>r is broken, and we<br />

assume that our Person crawls out of the Eleva<strong>to</strong>r and exits the simulation.<br />

Software Engineering Observation 15.6<br />

In a software-development process, the analysis phase yields a requirements document (e.g.,<br />

our problem statement). As we continue the design and implementation phase, we discover<br />

additional issues that were not apparent <strong>to</strong> us at the analysis phase. As designers, we must<br />

anticipate these issues and deal with them accordingly. 15.6<br />

Software Engineering Observation 15.7<br />

One false assumption <strong>to</strong> make is that the system requirements remain stable (i.e., they provide<br />

all information necessary <strong>to</strong> build the system) throughout the analysis and design<br />

phases. In large systems that have long implementation phases, requirements can, and often<br />

do, change <strong>to</strong> accommodate those issues that were not apparent during analysis. 15.7<br />

If our Eleva<strong>to</strong>r works correctly, the Eleva<strong>to</strong>r travels for five seconds—specifically,<br />

invoking method sleep halts the Eleva<strong>to</strong>r’s thread for five seconds <strong>to</strong> simulate<br />

the travel. When the Eleva<strong>to</strong>r thread awakens, it sends eleva<strong>to</strong>rArrived events as<br />

described in Section 10.22. The eleva<strong>to</strong>rDoor receives this event and invokes method<br />

doorOpened (message 3.3) of the ridingPassenger, as in:<br />

public void doorOpened( DoorEvent doorEvent )<br />

{<br />

// set Person on Floor where Door opened<br />

setLocation( doorEvent.getLocation() );<br />

}<br />

// interrupt Person's sleep method in run method and<br />

// Eleva<strong>to</strong>r's ride method<br />

interrupt();<br />

Method doorOpened sets the ridingPassenger’s Location <strong>to</strong> the Floor at<br />

which the Eleva<strong>to</strong>r arrived, then calls the ridingPassenger thread’s interrupt<br />

method. The interrupt method terminates the sleep method invoked in method<br />

ride, method ride terminates, and the ridingPassenger leaves the Eleva<strong>to</strong>r,<br />

then exits the simulation. When the ridingPassenger exits the Eleva<strong>to</strong>r, the<br />

ridingPassenger releases the moni<strong>to</strong>r on the Eleva<strong>to</strong>r object, which allows the<br />

waitingPassenger <strong>to</strong> invoke the ride method and obtain the moni<strong>to</strong>r. Now, the<br />

waitingPassenger may invoke method ride <strong>to</strong> ride the Eleva<strong>to</strong>r.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!