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 />
the time of sampling it should be correctly packaged in a single object, which<br />
the inValue method (in this case) should return. From a software point of<br />
view we will later work with such objects (of type RTEvent), but creating that<br />
object in this example typically requires native or hardware support depending<br />
on the system and its timing requirements. In a pure Java and concurrent<br />
setting, some improvements can, however, be made. For example the clock<br />
can be read before and after sampling:<br />
1 long timestamp , t0, t1; float x;<br />
2 do {<br />
3 t0 = System.currentTimeMillis ();<br />
4 x = inValue();<br />
5 t1 = System.currentTimeMillis ();<br />
6 } while (t1-t0 > eps); // eps is small , possibly zero.<br />
7 timestamp = (t0+t1)/2; // Statistical improvement.<br />
The disadvantage is that on a heavily loaded system, with certain scheduling,<br />
it can take a quite long time until the do-loop finishes. Limiting the looping<br />
by using a for-statement instead would be another solution which can easily<br />
be accomplished by the reader.<br />
Despite the ideal setting of this example, we found all these problems.<br />
With measurement noise and input value quantization the situation gets even<br />
more severe.<br />
3.1.3 Sleeping<br />
By replacing the comment /*Busy waiting.*/ in the above example with a call<br />
to sleep(1) (defined in class Thread), the efficiency issue is solved. The purpose<br />
of sleep is to provide a way of informing the scheduler that other threads may<br />
run during that long time from now, and that the calling thread should not be<br />
rescheduled for execution within that time. By calling for instance sleep(500),<br />
a minimum discrete time delay of 500 is obtained, which means (due to the<br />
quantization in time as described above) a continues/real-time delay that is<br />
greater then (but not equal to) 499 ms.<br />
Assume you want to do something approximately every new second, such<br />
as updating a progress bar while downloading a file from Internet. We may<br />
have a piece of code like<br />
1 long t, t0 = System.currentTimeMillis ();<br />
2 while (!transferFinished ()) {<br />
3 t = System.currentTimeMillis ();<br />
4 displayProgress(t0, t);<br />
5 Thread.sleep (1000);<br />
6 }<br />
If this code is in a subclass of Thread, the Thread qualification before sleep can<br />
of course be omitted. Since sleep is a static method, it can be called without<br />
having an instance of a thread object. In fact, even if you have a thread object<br />
threadOne, and call threadOne.sleep(1000) from another thread object (or any<br />
object), the calling thread (i.e., the thread of execution, not the thread object)<br />
will be put to sleep while threadOne is not effected. This is because sleep<br />
46 2012-08-29 16:05