JAVA-BASED REAL-TIME PROGRAMMING
JAVA-BASED REAL-TIME PROGRAMMING
JAVA-BASED REAL-TIME PROGRAMMING
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
2.4. Concurrency<br />
Behind the term concurrent programming is the fact that our software<br />
has to handle concurrently occurring external events. But before making a<br />
definition of the term, let us review some alternative approaches to handle the<br />
situation.<br />
2.4.1 Event processing<br />
In the simplest case, each input event can be immediately and fully handled<br />
before any following events must be taken care of. Then our program may<br />
consist of only data structures, so called event handlers which perform the<br />
processing of each event, and a so called dispatcher that calls the appropriate<br />
event handler for each incoming event. There would, for example, be one event<br />
handler taking care of mouse clicks which then would result in changes of the<br />
data structures, usually reflected on the screen. The dispatcher is called in a<br />
simple loop which thereby is ‘driving’ the application.<br />
The loop retrieving the events (or messages) and calling the handlers is (in<br />
Microsoft terms) called the message pump. When programming user interfaces<br />
using popular class libraries like MFC (Microsoft Foundation Classes), AWT<br />
(the Java-based Abstract Window Toolkit providing an interface to native<br />
graphics and window systems), or JFC (Java Foundation Classes, now known<br />
as Swing), the message pump is hidden inside the class library. Application<br />
programming then means writing the event handlers/listeners and registering<br />
them to the system.<br />
Event processing is easy to program but in most applications, and particularly<br />
in control applications like our LEGO-machine, this execution model<br />
alone is not sufficient. The reasons are:<br />
• Our single control loop will consume all available CPU time for the<br />
successive testing (called polling, or busy wait if nothing else is being<br />
done). This is waste of processing power which prevents the computer<br />
from simultaneously being used for other tasks.<br />
• Most control functions, but not this one, need a notion of time to work<br />
correctly. For example, if there would have been disturbances on our<br />
measurement signal, or if we would need to predict the temperature<br />
some time ahead, sampling and computing would require proper and<br />
known timing<br />
The straightforward solution now is to convert our control function into a<br />
form suitable for event processing, and to have control events periodically<br />
generated with a certain time period. This is actually a useful method for<br />
very small software systems, like software for so called micro-controllers. For<br />
our example application this means that we should:<br />
23