06.08.2013 Views

JAVA-BASED REAL-TIME PROGRAMMING

JAVA-BASED REAL-TIME PROGRAMMING

JAVA-BASED REAL-TIME PROGRAMMING

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.

3.1.6 Thread interruption and termination<br />

There is much to say about thread termination, but in short there are some<br />

simple rules to follow:<br />

1. Return from the run method terminates the thread. After the run<br />

method (which was called by the thread) returns or runs to its end,<br />

control is back into the system call that started the thread (due to<br />

the call of start). That call then completes by terminating the thread.<br />

Thereafter, calling the isAlive of the terminated thread object returns<br />

false.<br />

2. If one thread wants to terminate another thread, that other thread has<br />

to be prepared to commit suicide. Since there is no predefined methods<br />

for doing this, it has to be accomplished by the programmer, either by<br />

changing some state variable (such as a field/attribute named terminate),<br />

or by interrupting the thread which then (by convention) knows<br />

that it should die whenever interrupted.<br />

3.1. Threads<br />

3. The interrupt method is called in the manner “otherThread.interrupt();”<br />

to interrupt another thread. However, unlike hardware interrupts, the<br />

other thread is not really interrupted; it continues its execution as usual,<br />

except that a predefined internal state variable has registered the interrupt<br />

request.<br />

4. Each thread that should be possible to interrupt has to explicitly test<br />

if it has been interrupted. For this purpose, there is a static method<br />

Thread.interrupted() that returns true if interrupt has been called for<br />

the currently running thread. Note that this method also clears the<br />

interrupt flag. To check if another thread (i.e., not the currently running<br />

one) has been interrupted, isInterrupted() has to be called for that<br />

thread object. That does not effect the interrupted status.<br />

5. If a thread was interrupted when blocked on a blocking system call,<br />

such as sleep, it is scheduled immediately for execution but throwing an<br />

InterruptedException. In this case the interrupted flag is not set, but<br />

by catching the InterruptedException we can determine that the thread<br />

was interrupted. Thus, it is the execution of the catch clause that tells<br />

that the thread was interrupted, and the exception handling decides if<br />

the thread should resume its work or terminate (by returning from run).<br />

6. In accordance with the previous item, if a thread already has been interrupted<br />

when a system call (that throws InterruptedException) is issued,<br />

the exception is thrown immediately without blocking.<br />

Due to these rules, the standard way of implementing a thread is to have a<br />

run method looking like:<br />

51

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

Saved successfully!

Ooh no, something went wrong!