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.
Real-Time Events – RTEvent<br />
3.4. Message-based communication – Mailboxes<br />
The class se.lth.cs.realtime.event.RTEvent is the EventObject base class<br />
for real-time events. Real-time here basically means that events are timestamped,<br />
whereas true real-time properties have to do with the properties of<br />
the executing threads and their scheduling. Hence, real-time events will be<br />
needed by real-time threads (introduced in a later chapter), but they are also<br />
useful for multi-threaded programming and for the communication between<br />
concurrent and real-time threads, so introducing the RTEvent class here for<br />
message-based concurrency is natural.<br />
Every RTEvent comes from a source object referred to in the EventObject<br />
base class. Additional IDs, such as sequential numbers or the kind of event<br />
within a certain type, are expected to be defined in subclasses together with<br />
other user attributes. The time stamp is, however, fundamental to real-time<br />
systems as a mean to denote the freshness of the data. It is therefore an<br />
attribute here and the motivation for this class.<br />
The source attribute, typically being a thread object, is not supposed<br />
to keep any reference to the event after posting it. Therefore, methods of<br />
this class should not need to be synchronized. To support detection of kept<br />
references, for instance when the event is sent again, there is a field owner<br />
which is set to the same as source when the event is created. When an event<br />
is transferred, for instance via a buffer, the ownership changes to the buffer<br />
and then to the receiving thread. Using available classes, the user should not<br />
need to care about the ownership, it is checked to detect certain programming<br />
errors. Sometimes, however, it is desirable to keep a set of event objects that<br />
are sent several times, for instance every time a certain state transition occurs.<br />
In such cases the specific event subclasses are supposed to set the (protected)<br />
owner field to null, which disables the checking of ownership.<br />
Refer to the online class documentation for further details. In short, neglecting<br />
a few features for later chapters, the se.lth.cs.realtime.RTEvent<br />
class looks as:<br />
public abstract<br />
class RTEvent extends EventObject {<br />
protected long timestamp; // Creation time in ms.<br />
protected volatile transient Object owner; // Responsible thread.<br />
}<br />
public RTEvent(); // Use current Thread & TimeMillis.<br />
public RTEvent(Object source); // Set source , default timestamp.<br />
public RTEvent(long ts); // Set timestamp , default source<br />
public RTEvent(Object source , long ts); // Override both defaults.<br />
public final Object getOwner()<br />
public double getSeconds()<br />
public long getMillis()<br />
public long getNanos()<br />
There is also a setOwner method, but that method is declared final and has<br />
package visibility so it is of no interest for user code. Still, the user can in<br />
89