This IBM developerWorks article states:
“The one time it is acceptable to swallow an interrupt is when you know the thread is about to exit. This scenario only occurs when the class calling the interruptible method is part of a
Thread
, not aRunnable
[…]”.
I always implemented Runnable
for my threads by now. Giving a Runnable
implementation like this:
public class View() implements Runnable {
@Overload
public void run(){
Thread worker = new Thread(new Worker());
worker.start();
do{
try{
TimeUnit.SECONDS.sleep(3);
updateView();
}catch(InterruptedException e){
worker.interrupt();
// Thread.currentThread().interrupt();
return;
}
}while(true);
}
protected void updateView(){
// …
}
}
Is it really necessary to call Thread.currentThread().interrupt();
right before my return;
statement? Doesn’t return;
perform a clean enaugh exit already? What’s the benefit of calling it? The article states that it should be done because otherwise “[…] code higher up on the call stack won't be able to find out about it […]”. What’s the benefit of a thread in Thread.State.TERMINATED
with interrupted flag set over one without it upon application shutdown? Can you give me an example where code outside the Runnable
inspects the interrupted flag for a sensible reason?
BTW, is it a better code design to extend Thread
instead of implementing Runnable
?