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

break the rules. Before doing that, be aware of the previos section and the<br />

following:<br />

1. As a way of doing manual performance optimization, small get and set<br />

methods (belonging to a monitor with other methods synchronized) are<br />

often not declared synchronized. However, the way it is done is (as<br />

far as the author has seen) mostly wrong, resulting in very-hard-to-find<br />

bugs when the program is run on some platforms. Therefore, keep the<br />

following in mind:<br />

• Only access data that is consistent all the time. Even if getting<br />

an integer can be done atomically, that attribute may have an inconsistent<br />

value due to computations in the synchronized methods.<br />

In a non-final class with non-private attributes, you cannot know<br />

what subclasses will do.<br />

• Only access a single single-word value (built in type or object reference)<br />

by a non-synchronized monitor method, and if not declared<br />

volatile, make sure that the updated value is obtained (by other<br />

system calls resulting in memory being updated, or by only permitting<br />

a single-threaded write during startup or similar). Even if<br />

you figure out that several attributes can be changed due do the<br />

logic of other methods, the risk is too high that errors are introduced,<br />

for instance when the class is extended or enhanced. An<br />

acceptable single-word access could be an object reference that is<br />

final (set in constructor) but needs to be frequently obtained by<br />

other threads.<br />

• Attributes (except those of types double or long that need synchronization<br />

to be portable in any case) accessed without synchronization<br />

should be declared volatile. According to the Java language<br />

specification, all types except long and double are atomically accessed.<br />

For object references it is of course crucial that the address<br />

value does not get corrupted, which would break the built-in safety<br />

(the so called sandbox model) of Java. The only data-type you can<br />

assume being atomically accessed in native code on all platforms<br />

is the byte, so implementation of native methods (and virtual machines<br />

and Java native compilers) requires some care, in particular<br />

for CPUs with word length less than 32 bits.<br />

• Comment your assumptions and decisions when you are doing this<br />

type of (what you think is) clever programming.<br />

Hence, be careful when skipping synchronization; such optimization is<br />

better done by compilers and class loaders.<br />

2. When developing software libraries or components that should stand<br />

also improper usage, such as a synchronized block locking a monitor<br />

72 2012-08-29 16:05

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

Saved successfully!

Ooh no, something went wrong!