JAVA-BASED REAL-TIME PROGRAMMING
JAVA-BASED REAL-TIME PROGRAMMING
JAVA-BASED REAL-TIME PROGRAMMING
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
• If the operation of an industrial robot is interrupted because it detects<br />
a human worker within its working range, ongoing motions have to stop<br />
but the work-piece should not be dropped, which requires engineered<br />
exception handling and not simply termination of the thread.<br />
Still, it is better to throw an Error which is much more likely to reach the<br />
outer level of error handling despite ’lazy’ (or ’too busy’) programmers. Do<br />
like in the code example above.<br />
Aliveness<br />
Since interrupt is more of an order to commit suicide than actually interrupting,<br />
the interrupted thread may continue for quite a while before it is<br />
terminated/dead. It is sometimes useful to know when the thread really has<br />
stopped its execution. That is done by calling the isAlive method of the thread<br />
object. A live thread object is what we call an active object.<br />
Notice that even if a thread has terminated, the object as such remains<br />
(as the remains :-) but after termination, even when isAlive returns false, it<br />
cannot be brought back to life by calling start again. Thus, a thread object<br />
can only be started once. However, trying to restart a terminated thread is<br />
silently accepted with no errors or exceptions; you have to keep track of the<br />
thread status yourself.<br />
If calling isAlive returns true, the thread was alive at the time of the call<br />
but it could be dead right after if it was just about to terminate; we would<br />
need to lock the object and check both aliveness (via isAlive) and termination<br />
status (as determined by isInterrupted and user code) if we need to be sure<br />
that the thread still is running (accepting new orders etc.). If calling isAlive<br />
returns false, the thread either has not been started, or it has terminated. If<br />
a call of start results in isAlive returning true, it was not started. Otherwise,<br />
it was terminated.<br />
Joining<br />
You normally do not call isAlive because normally a thread is started right<br />
after the thread object was created, and if you need to await the thread<br />
termination you preferably suspend execution until that happens (not wasting<br />
CPU-time on polling the aliveness status). For this purpose, there is a method<br />
join which blocks the caller until isAlive of that thread object returns false.<br />
Details:<br />
• Calling join of a thread object which already has terminated simply<br />
returns without blocking.<br />
• A thread object calling its own join method (that is, this.join() or<br />
currentThread().join()) will put itself into a state that we could call<br />
3.1. Threads<br />
53