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 />

choice would be to let every potentially blocking monitor methods be declared<br />

to throw an InterruptedException. However, in particular when there<br />

are several levels of calls, this soon gets very messy. Furthermore, the monitor<br />

method cannot complete its operation if the condition is not fulfilled, and<br />

it cannot simply return before the operation is completed either (both these<br />

alternatives would violate the correctness of the monitor).<br />

So, the only reasonable alternative is to throw a Throwable object that<br />

we do not need to catch or declare. Java provides two alternatives for doing<br />

that: throwing an RuntimeException which is an Exception, or throwing an<br />

Error. To emphasis that the normal user code is not supposed to catch an<br />

interrupted wait (since it in most cases needs to propagate to the outermost<br />

level of the thread), throwing an Error is the appropriate solution. That is, we<br />

throw an instance of RTInterrupted, which is a subclass to java.lang.Error.<br />

This solution corresponds to the SemViolation as described on page 65 for<br />

semaphores.<br />

The implication is that we normally call wait according to:<br />

{ //.....<br />

while (!ok) {<br />

try {<br />

wait();<br />

} catch (InterruptedException exc) {<br />

throw new RTInterrupted(exc);<br />

}<br />

}<br />

}<br />

Note that when an InterruptedException is thrown, the interrupted-flag of<br />

that thread is cleared. Hence, it is only the fact that we get to the catch<br />

clause that tells us that the thread is interrupted, and after the catch it is<br />

only the thrown RTInterrupted that carries the interrupt information when<br />

the call stack is popped.<br />

If we use a framework that is predefined to catch InterruptedException<br />

specifically, an RTInterrupted can of course be converted back to an InterruptedException<br />

by catching the error and throwing a new InterruptedException<br />

(or getting the original exception by calling getCause, see the exceptionchaining<br />

of java.lang.Throwable in JDK1.4 and later).<br />

Sometimes interrupting a thread is used to actually interrupt a command<br />

or action in an almost expected way. That is, instead of cluttering the code<br />

with a lot of tests if the operation is to be aborted, we can let execution<br />

continue until it reaches a blocking call and then utilize the thrown RTInterrupted.<br />

For instance, a robot performing its task according to a sequence of<br />

instructions can be interrupted by the operator who issues another task or<br />

simply stops the robot due to some fault. The thread that handles operator<br />

input can then interrupt the ongoing robot work, and since computers are<br />

faster than mechanics the work thread will be blocked on getting a new order<br />

or on the completion of last order. By catching InterruptedException and<br />

82 2012-08-29 16:05

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

Saved successfully!

Ooh no, something went wrong!