06.08.2013 Views

JAVA-BASED REAL-TIME PROGRAMMING

JAVA-BASED REAL-TIME PROGRAMMING

JAVA-BASED REAL-TIME PROGRAMMING

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

4. Exercises and Labs<br />

your washing program subclasses. The washing program is implemented in<br />

the abstract method wash called from the run method.<br />

public abstract class WashingProgram extends RTThread {<br />

/**<br />

* @param mach The washing machine to control<br />

* @param speed Simulation speed<br />

* @param tempController The TemperatureController to use<br />

* @param waterController The WaterController to use<br />

* @param spinController The SpinController to use<br />

*/<br />

protected WashingProgram(AbstractWashingMachine mach ,<br />

double speed ,<br />

TemperatureController tempController ,<br />

WaterController waterController ,<br />

SpinController spinController) {<br />

super();<br />

}<br />

myMachine = mach;<br />

mySpeed = speed;<br />

myTempController = tempController;<br />

myWaterController = waterController;<br />

mySpinController = spinController;<br />

/**<br />

* This run() method does two things.<br />

* <br />

* Call the wash() method implemented in the subclass.<br />

* If wash() was interrupted , turn all off machinery.<br />

* <br />

*/<br />

public void run() {<br />

boolean wasInterrupted = false;<br />

try {<br />

wash();<br />

}<br />

catch (InterruptedException e) {<br />

wasInterrupted = true;<br />

}<br />

catch(RTInterrupted e) { // Thrown by semaphores<br />

wasInterrupted = true;<br />

}<br />

}<br />

if (wasInterrupted) {<br />

System.out.println("Washing program aborted.");<br />

myTempController.putEvent(<br />

new TemperatureEvent(this , TemperatureEvent.TEMP_IDLE , 0.0));<br />

myWaterController.putEvent(<br />

new WaterEvent(this , WaterEvent.WATER_IDLE , 0.0));<br />

mySpinController.putEvent(<br />

new SpinEvent(this , SpinEvent.SPIN_OFF));<br />

}<br />

/**<br />

* Called by run() to perform the actual washing stuff.<br />

* Should be implemented by the subclass. Does not need to<br />

* catch InterruptedException.<br />

*/<br />

abstract protected void wash() throws InterruptedException;<br />

128 2012-08-29 16:05

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

Saved successfully!

Ooh no, something went wrong!