06.08.2013 Views

JAVA-BASED REAL-TIME PROGRAMMING

JAVA-BASED REAL-TIME PROGRAMMING

JAVA-BASED REAL-TIME PROGRAMMING

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

3.1. Threads<br />

first obtains the currently executing thread, which you can also do by calling<br />

Thread.currentThread(), and then that thread (the caller of course) will sleep.<br />

In other words, Thread.sleep(t) is the same as Thread.currentThread().sleep(t),<br />

and a thread can put itself to sleep but cannot put another thread to sleep. If<br />

that should have been possible, certain restrictions on run-time systems and<br />

code optimizations would need to be imposed, resulting in decreased (possible)<br />

performance.<br />

Note that sleep(1000) above does (in most cases) not give an update every<br />

second; the 1000 is a minimum sleeping time but there is no strict upper limit.<br />

The actual time before execution continues after sleep typically depends on<br />

the processor load. Furthermore, the sleeping for 1000 ms is from the time<br />

that called starts executing in the JVM or OS with interrupts disabled. Any<br />

time increments before then results in that much longer period of time for<br />

the update. Most often, especially in control applications, it is a periodic<br />

behaviour that is desired.<br />

We have a similar situation if we in our example wants to call displayProgress<br />

every second. Still we do not impose any upper limit on the waiting for each<br />

new second (which would be a real-time requirement), we just require no<br />

long-term drift as long as the computer is not permanently overloaded. This<br />

is accomplished in the following:<br />

1 long t, t0, diff;<br />

2 t = t0 = System.currentTimeMillis ();<br />

3 while (!transferFinished ()) {<br />

4 displayProgress(t0, t);<br />

5 t += 1000;<br />

6 diff = t - System.currentTimeMillis ();<br />

7 if (diff > 0) Thread.sleep(diff);<br />

8 }<br />

Note that t now is ideal, not reflecting temporary excessive delays. To<br />

facilitate implementation of periodic activities, there should of course have<br />

been a method sleepUntil in class Thread, but there is not. Even if it can<br />

be emulated as shown, an increment of the real time after assignment of diff<br />

but before calling sleep results in a requested sleeping time that is too long.<br />

That does not really matter since we can be delayed after the sleeping anyway.<br />

(But from a real-time systems point of view, it would be preferable to correctly<br />

inform the real-time scheduling.)<br />

3.1.4 Race conditions<br />

Knowing how to create multiple threads, let us make a multi-threaded version<br />

of the standard HelloWorld program. Consider the following example (where<br />

exceptions have been omitted for clarity):<br />

class HelloWorld extends Thread {<br />

public static void main(String[] arg) {<br />

System.out.print("Hello ");<br />

new HelloWorldApp ().start();<br />

sleep(1);<br />

47

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

Saved successfully!

Ooh no, something went wrong!