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.3. Objects providing mutual exclusion – Monitors<br />

converting it to an RTInterrupted in whatever monitor it occurs, we can then<br />

catch the RTInterrupted and terminate the thread like:<br />

class RobotDuty extends Thread {<br />

public void run() {<br />

try {<br />

while (doWork(Master.nextInstruction ())) {};<br />

} catch (RTInterrupted) { Master.workAborted ();} // Acknowledge.<br />

} // No more work; end of duty!<br />

}<br />

Terminating the thread (and creating a new one for the next work) like in this<br />

example is convenient for handling sessions that have a finite life time, such as<br />

processing a batch recipe in a chemical industry, or carrying out an e-business<br />

transaction; the state of the session is kept by the thread, and both the activity<br />

and the state gets garbage-collected when the session is finished. In feedback<br />

control, on the other hand, we usually create the threads that execute during<br />

the entire lifetime/uptime of the system; the state of the controlled system<br />

needs to be maintained also over mode changes and the like, and the control<br />

can be sensitive to delays during creation of new threads.<br />

3.3.3 More on monitor programming<br />

The following applies to the use of standard Java packages, whereas enhanced<br />

support for monitors will be introduced in Chapter 4.<br />

Static monitors – locking class variables<br />

Executing synchronized code results in the synchronized object being locked.<br />

That is, mutual exclusion is provided for the object (not the class) and concurrent<br />

access to different objects of the same class is, of course, still permitted.<br />

However, there may be attributes that are shared between all objects of a<br />

certain type, so called static attributes or class variables. Mutual exclusion<br />

then requires special care. Even if the object provides mutual exclusion on its<br />

attributes, for instance by having all attributes private and all non-static methods<br />

synchronized, the class variables are still not locked. The reason is that<br />

the class as such is another object which needs to be locked separately, either<br />

by a static synchronized method or by a synchronized(static_attribute){}<br />

block. In the same way, since a static method has no reference to ’this’ object,<br />

a static method does not provide mutual exclusion for any non-static methods.<br />

Hence, locking for classes (static attributes) and objects are different things,<br />

implying that a inside a synchronized method locking either the class or the<br />

object, you have to put a synchronized block for explicitly locking the other,<br />

if both the class and object need to be locked at the same time.<br />

83

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

Saved successfully!

Ooh no, something went wrong!