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

’coma’; alive sleeping until the death, unless interrupted which will permit<br />

recovering.<br />

• Joining a thread object that has not been started returns directly (without<br />

errors) since it is not alive (yet).<br />

In practice, in your own code if you are aware of the limitations, thread termination<br />

is not a problem. To specify a time-out for joining a thread, there<br />

are versions of join with time-out arguments.<br />

Inappropriate thread methods<br />

Among the useful and well designed methods of class Thread, there are also<br />

some ’features’ that resembles ’bugs’ and should be avoided.<br />

First there is a method destroy which is supposed to actually interrupt and<br />

kill the thread. However, allocated resources could then not be returned since<br />

that (for run-time reasons) has to be done by the thread that is going to be<br />

killed. Since destroy most likely would leave the application in a badly working<br />

state, it should not be called. It is even written in the Java2 documentation:<br />

“This method is not implemented”.<br />

There is also a method stop which in the Java2 API documentation is<br />

described as “inherently unsafe ..., potentially resulting in arbitrary behavior”.<br />

Its purpose is/was to force the thread to stop executing. It is unsafe for the<br />

same reasons as destroy is. Actually calling stop will permit some cleanup<br />

and return of resources to be done, but that exhibits undefined timing and<br />

resource management on the user level cannot (in practice) be handled in a<br />

system routine such as stop.<br />

In the beginning of the real-time programming subject, students often<br />

want to use something like stop (or even destroy) instead of dealing with<br />

actual execution and OS properties. It is hard to understand why the Java<br />

engineers @sun.com introduced stop in the first place.<br />

Finally, there are two methods that could have been useful if designed<br />

properly: suspend and resume. The problem is that, as their names suggest,<br />

suspending and resuming execution was accomplished as a mechanism well<br />

separated from the ordinary thread management. With two unrelated ways of<br />

suspending execution, there was a big risk for a set of threads to be (depending<br />

on timing) permanently blocked. These methods and their deficiencies are<br />

outside the scope of this text.<br />

3.1.7 The Thread class<br />

We are now ready to sum up the content of class lava.lang.Thread which in<br />

a abbreviated form can be expressed as shown below(all shown members are<br />

public which is omitted, and deprecated methods are not shown). The task<br />

to be carried out by the thread is implemented in the run method, either by<br />

54 2012-08-29 16:05

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

Saved successfully!

Ooh no, something went wrong!