I'm reading Goetz's Java Concurrency In Practice where this example code is shown:
public final class Indexer implements Runnable {
private final BlockingQueue<File> queue;
public Indexer(BlockingQueue<File> queue) {
this.queue = queue;
}
@Override
public void run() {
try {
while (true) {
queue.take();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
with the description:
Restore the interrupt. Sometimes you cannot throw InterruptedException, for instance when your code is part of a Runnable . In these situations, you must catch InterruptedException and restore the interrupted status by calling interrupt on the current thread, so that code higher up the call stack can see that an interrupt was issued, as demonstrated in Listing 5.10 .
In the example code, "code higher up the call stack" would never see an interrupt if this code executed - or am I making the wrong deduction? The thread here just dies after calling interrupt()
, correct?
So the only way this interrupt()
could be useful is if it in within a loop, correct?