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

Performed(event) of the event listeners that implement the ActionListener<br />

interface and have been registered via the Button.addActionListener(listener)<br />

method.<br />

For programming of embedded systems in general we should of course not<br />

depend on any graphics-related packages or classes (since embedded processors<br />

usually do not provide a GUI, and they are subject to restrictions on available<br />

memory) for the core operation of the system. (Compare with Linux which is<br />

very small without the graphics, compared to a full installation with window<br />

managers and the like.) We will, however, make use of the delegation-based<br />

event model; you subscribe on the events you want to receive so the sender<br />

does not have to perform any mass distribution which would be a waste of<br />

resources.<br />

In the packages mentioned above, emitting an event is usually referred to as<br />

firing it. To fire an event will be referred to as synchronously calling the event<br />

listeners, whereas to post an event will be referred to as asynchronously buffer<br />

the event for later processing by the receiving thread. This agrees, although<br />

not stated explicitly, with conventions used in various graphics packages, even<br />

if posting is normally handled internally and the user code is assumed to process<br />

each fired event in a single-threaded manner (to improve performance<br />

and to reduce complexity/concurrency). That requires that all listeners can<br />

process their events instantly, otherwise the entire GUI would ’freeze’ during<br />

that period of processing or waiting. For the situation when waiting<br />

actually have to be done, for instance waiting for a download of a file to be<br />

completed after a download button has been pushed, concurrent GUI-based<br />

processing can be requested and managed via the javax.swing.SwingUtilities<br />

and javax.swing.Timer classes. In SwingUtilities, you have to use the methods<br />

invokeLater and invokeAndWait, which take a Runnable argument. That<br />

Runnable is run concurrently with the built-in GUI event-dispatching thread,<br />

but in such a way that it does not interfere with the swing/AWT and the<br />

user’s event processing code, which concerning the GUI mainly stays singlethreaded.<br />

In multi-threaded programming threads need to operate asynchronously<br />

(although synchronizing when accessing shared data), so the emitting thread<br />

needs to post the events/messages to some buffering object where the receiving<br />

thread can fetch the events/messages.<br />

For threads running in different virtual (or physical) machines, sending<br />

a message from one thread to another cannot be accomplished by simply<br />

passing a reference to an event object between the threads. Recall that object<br />

references (which are pointers within the address space of the OS-process) have<br />

a meaning only within the program. For multi-threaded programs (within one<br />

JVM/OS-process) on the other hand, we can build upon the EventObject class<br />

mentioned above. To prepare for real-time programming, however, we need a<br />

new base class with some additions for real-time event objects.<br />

88 2012-08-29 16:05

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

Saved successfully!

Ooh no, something went wrong!