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.4.2 Events and buffers<br />

3.4. Message-based communication – Mailboxes<br />

Assuming the sending and receiving thread belong to the same program, which<br />

is the case in this entire chapter, the sender and receiver can refer to shared<br />

objects. Thus, the buffer we need can be a monitor. We may compare with<br />

messages within user interaction packages such as the AWT. Such messages<br />

are called events since they represent an application event, but they are not<br />

buffered except before being fed into the application. Additionally, the AWT<br />

InputEvents are time-stamped so the graphics classes and the application<br />

can react accordingly to when the event (such as a mouse click) occurred<br />

physically, which can be quite different from when the event is processed (in<br />

particular on a heavily loaded system).<br />

For instance, assume two mouse clicks should be considered a a doubleclick<br />

if there is less than 1 s between them, and that the user clicks twice with<br />

the second click 0.9 s after the first. Then if there is a 0.2 s delay right after<br />

processing/registering the first click, the second click will be processed 1.1 s<br />

after the first one. Using the time-stamps it can still be deduced that the user<br />

actually double-clicked.<br />

Similar situations appear in feedback control where the delay (difference<br />

between current time and event time) results is so called phase lag, which<br />

in turn results in bad damping or even instability. Since our aim is both<br />

concurrent and real-time programming, time-stamping our messages (at the<br />

time of creation) will always be done.<br />

Events as messages<br />

The emission of an event object denotes some explicit change in state or execution<br />

of an application. Since the JDK1.0 there is the class java.awt.Event<br />

which was used as the base class for all the events in the old AWT1.0 containmentbased<br />

event model. That model was to a large extent implemented by inheritance.<br />

It was convenient when programming small applets, but did not scale<br />

up at all to larger application which implied extensive traversals of the containment<br />

hierarchy for each event (such as a mouse click). The java.awt.Event<br />

is kept only for backward compatibility and should not be used.<br />

Since JDK1.1, the so called delegation-based event model is used, both in<br />

the AWT (java.awt) as well as in Swing (JFC; javax.swing) and Java Beans<br />

(java.beans). The base class for events in all these packages is the (package<br />

and application area neutral) java.util.EventObject. The delegation model<br />

means that event handlers register themselves as listeners (synchronous subscribers)<br />

to certain events, and the emitter of the events calls some eventprocessing<br />

method according to the interface for that particular type of event.<br />

For instance, clicking with the mouse on a button in the GUI (Graphical<br />

User Interface) results in an InputEvent into the Java GUI package (e.g.<br />

AWT) where the associated java.awt.Button object calls the method action-<br />

87

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

Saved successfully!

Ooh no, something went wrong!