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.

3. Multi-Threaded Programming<br />

public void run() {<br />

while (!isInterrupted ()) { // !interrupted() OK in most cases.<br />

// here is where the actual code goes....<br />

}<br />

} // End of life for this thread.<br />

After the loop, isInterrupted can be used to check how the loop was exited.<br />

Instead of terminating after an interrupt, you can have an outer loop to restore<br />

the state and continue execution. Additionally, you may need to catch<br />

InterruptedException, or other Throwable objects such as those of type Error,<br />

either outside the while loop above or at several places inside that loop depending<br />

on the code. Note, however, you should normally not try to catch<br />

and recover from an InterruptedException thrown from a blocking call that<br />

have to do with access or allocation of shared resources/data, if resources are<br />

not left in a consistent state (bank transaction not half made, etc.). When<br />

resuming normal operation after such a call has been interrupted, there is a<br />

big risk that execution continues as if resources where locked even if that is<br />

not the case. Having each method to pass the exception (instead of catching<br />

it locally) to the caller, by appending throws InterruptedException to its signature,<br />

messes up the code too much. In practice, the result is often empty<br />

catch-clauses with an intention to add the code later, which then never is<br />

done and the application silently compiles and runs, but interrupting it may<br />

result in disaster. The recommended approach therefore is to always convert<br />

an InterruptedException to an Error. For this purpose, we will later use the<br />

class RTInterrupted which is a subclass of Error with some enhancements:<br />

try {<br />

argout = methodThrowingInterruptedException(argin);<br />

} catch (InterruptedException exc) {<br />

throw new RTInterrupted(exc.toString ());<br />

}<br />

which means that the method containing the above code is not defined to<br />

throw any InterruptedException, and the caller(s) of that method do not<br />

need to be cluttered with try-catch clauses. Recall that the intended use of<br />

the Error class is for throwing severe exceptions that the user is not expected<br />

to catch, but he/she can catch them if that is needed. Hence, throwing an<br />

error when a resource allocation request is interrupted agrees well with the<br />

Java API conventions.<br />

A different case is when an independent sequence of operations is to be<br />

interrupted. Sequencing on an application level (that is, advancing the state<br />

of the application, not caring about advancing the program counter for each<br />

instruction) is typically done by waiting for time to pass, or on certain conditions<br />

to be true. Interrupting such a thread, and letting the interrupted<br />

thread handle the Exception/Error, can be a perfectly acceptable thing to do:<br />

• If the interrupted sequence is a web-server transaction, closing files and<br />

returning any resources in their initial state should be possible and<br />

straight forward.<br />

52 2012-08-29 16:05

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

Saved successfully!

Ooh no, something went wrong!