I am very confused and cannot understand why InterruptedException should not be swallowed.
An IBM article says:
When the lock method detects an interrupt and throws an InterruptedException, it clears the interrupted status. If you catch an InterruptedException but cannot recover it, you must save evidence that an interrupt has occurred so that the code above in the call stack can learn about the interrupt and respond to it if it wants to
public class TaskRunner implements Runnable { private BlockingQueue<Task> queue; public TaskRunner(BlockingQueue<Task> queue) { this.queue = queue; } public void run() { try { while (true) { Task task = queue.take(10, TimeUnit.SECONDS); task.execute(); } } catch (InterruptedException e) { Thread.currentThread().interrupt();
In addition, Java Concurrency in practice discusses this in more detail in Chapter 7.1.3: โInterrupt Responseโ. His rule:
Only code that implements a thread interrupt policy can swallow an interrupt request. The target code and general purpose libraries should never swallow interrupt requests.
1. Can someone explain how code in a higher call stack uses the status set by Thread.currentThread (). interrupt (); in the catch block when the thread terminates?
Also explain the rule above?
source share