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.
3. Multi-Threaded Programming<br />
user interface using the javax.swing classes and only some threading for handling<br />
the network will probably inherit graphical properties and implement<br />
Runnable when needed. For an embedded control application, on the other<br />
hand, threading (and timing) is a primary issue and inheritance from class<br />
Thread is a natural choice.<br />
Some programmers prefer to always use the Runnable alternative as their<br />
standard way of doing things, whereas others always create Thread subclasses<br />
(sometimes having an internal thread object when there is a need to inherit<br />
from other classes). In the sequel, threading is the primary issue so we will<br />
mostly use Thread as a base class, but utilizing interface Runnable is covered<br />
as well.<br />
3.1.2 Time<br />
Even though concurrent programs do not ensure timing, it is very useful<br />
to let actions depend on real time, i.e., reading the real-time clock which<br />
is available on any useful computing platform. In Java the default time<br />
unit is milliseconds, and the current clock time is obtained by calling System.currentTimeMillis<br />
which returns the time in milliseconds counted from<br />
beginning of 1970. Since the return value is of type long, the y2k type of<br />
problem that comes with the finite time range is quite far in the future (year<br />
292272993, which should mean no problem for most applications).<br />
The advantage with currentTimeMillis stored in a built in numeric datatype<br />
(long) is that it can be conveniently used for computations. For humanreadable<br />
time Java also provides a Date class which also copes with time<br />
zones, locales, and the like. That is, however, a matter of formatting and for<br />
our needs it is the system time in milliseconds that is relevant.<br />
Assume we want to compute the trend (or numerical derivative) of an<br />
analog input signal. Further assume we have a method inValue that handles<br />
the AD conversion and returns a scaled value as a float, and let us say that we<br />
have an infinite resolution in value. Concerning resolution in time, we have the<br />
millisecond time described above. That time is updated via a clock interrupt<br />
routine in the JVM or OS. Some embedded computers are equipped with a<br />
high resolution real-time clock, but using it would require a native method<br />
and loss of portability. Using only the standard 1 ms resolution, an attempt<br />
to compute the trend could be:<br />
1 float trend() {<br />
2 float x, x0 = inValue(); // [V]<br />
3 long t, t0 = System.currentTimeMillis ();<br />
4 while ((t=System.currentTimeMillis ())==t0) {/*Busy waiting.*/};<br />
5 x = inValue();<br />
6 return (x-x0)/(t-t0)*1000; // [V/s]<br />
7 }<br />
With an input signal that increases constantly with 1 V/s we would like to<br />
obtain the value 1.0, but already with such a small piece of software it is hard<br />
44 2012-08-29 16:05