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

via synchronized methods. However, the concurrency correctness would then<br />

heavily depend on programming discipline; neither the compiler nor the runtime<br />

system can complain about the run method accessing the attributes (run<br />

is nothing special from a language point of view). The public, protected, and<br />

private, keywords have to do with visibility between ordinary objects; they<br />

have nothing to do with concurrency. In case of mixing threads and monitors<br />

we get no help from the compiler as when we use synchronized methods to<br />

access protected attributes. The lack of support for the Task abstraction<br />

could be considered as a deficiency of Java, but as will be argued later, the<br />

task is not a suitable abstraction for the design of object oriented concurrent<br />

(or real-time) systems anyway.<br />

Use and misuse of keyword volatile<br />

To further descriebe the special need for concurrent software to be wellstructured<br />

the following code snippet contains a synchronized method in a<br />

thread object. By breaking rule A above we may say that we have one error,<br />

and there is a risk that mutual exclusion will not work, but what other<br />

problems can result?<br />

/*<br />

* Find four faults , concerning concurrency , in this class.<br />

*/<br />

import I.robot.dest; // Import static robot destination class Dest.<br />

public class PathCoord extends Thread {<br />

private double x,y;<br />

public synchronized int dist() {return Math.sqrt(x*x+y*y);}<br />

public void int run() {<br />

double path = 0.0; // Path coordinate , locally on thread stack.<br />

while(!isInterrupted ()) {<br />

path = Dest.step(path); // Local data and reentrant method.<br />

x = Dest.fx(path); // Calling one thread -safe method ,<br />

y = Dest.fy(path); // and calling another one.<br />

Dest.nextStep(x,y); // Use x,y and wait for next sample.<br />

}<br />

}<br />

}<br />

Even if this program is run on 64-bit machine (with the doubles being atomically<br />

assigned, more on that below), it can happen that a valid value x is<br />

not consistent with a valid value y (together defining an invalid coordinate<br />

outside the path for the destination of the robot motion). This is a concurrency<br />

error due to preemption (between or in the calls of fx and fy, since run<br />

as usually is not synchronized), independently of the types of x and y, and<br />

despite that fx and fy each are thread safe. Hence, this is a concurrency fault<br />

and the outcome of running the software is dependent on the execution timing<br />

and scheduling. In combination with breaking the rule not to mix threads<br />

and monitors, we may say this constitutes two errors in this class. However,<br />

there are also two other concurrency problems that are common in embedded<br />

software since most programmers also in industry are not aware of this:<br />

70 2012-08-29 16:05

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

Saved successfully!

Ooh no, something went wrong!