JAVA-BASED REAL-TIME PROGRAMMING
JAVA-BASED REAL-TIME PROGRAMMING
JAVA-BASED REAL-TIME PROGRAMMING
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
3. Multi-Threaded Programming<br />
decouple the execution, that is, to make object interaction asynchronous but<br />
still based on the methods at hand. Hence, the normal case is to hide the<br />
mailbox as an attribute inside the object. There is a subclass of Thread that<br />
provides that feature, namely class se.lth.cs.realtime.JThread.<br />
The featured Java Thread class – JThread<br />
To support the most common case that the mailbox in terms of an RTEventBuffer<br />
is handled locally within the receiving thread, and encapsulated by the methods<br />
of that thread, class se.lth.cs.realtime.JThread contains the following<br />
(see volatile on Page 72):<br />
public class JThread extends java.lang.Thread {<br />
/** The event inbut buffer. */<br />
protected volatile RTEventBuffer mailbox;<br />
}<br />
/**<br />
* Put the time -stamped event in the input buffer of this thread.<br />
* If the buffer is full , the caller is blocked.<br />
*/<br />
public RTEvent putEvent(RTEvent ev) {<br />
mailbox.doPost(ev); // Subclasses may use tryPost.<br />
return null; // Subclasses may return resulting event.<br />
}<br />
// etc. etc.<br />
The J in JThread is to stress the fact that this type of thread still is a (concurrent)<br />
Java thread (that is, not specified to be real time as will be the case<br />
in following chapters).<br />
To simplify programming in the case that the same thing should be repeated<br />
over and over again, there is a default run method that calls perform<br />
according to the following:<br />
public void run() {<br />
while (!isInterrupted ()) {<br />
perform();<br />
}<br />
}<br />
When implementing the thread behavior, in run or perform, the the doFetch<br />
and tryFetch methods are useful, like is this exemplifying perform method:<br />
class MyEventProcessor extends JThread {<br />
public void perform() {<br />
serverObject.process(mailbox.doFetch ());<br />
}<br />
}<br />
In addition to the methods of the base class Thread as shown on Page 55, this<br />
means that the class JThread provides the following simple to implement but<br />
convenient methods:<br />
RTEvent putEvent(RTEvent ev) // Generic event input<br />
void terminate() // Interrupt and join<br />
static void sleepUntil(long wakeUpTime) // Sleep until that absolute time<br />
void perform() // Override to define cyclic work<br />
96 2012-08-29 16:05