I think that Thread.interrupt()
sounds more intrusive than it really is. Thread interruption in Java is entirely advisory. Generally a task does not need to go to any effort to avoid being interrupted. Rather, it usually takes some real effort to properly observe the interrupt status in order to exit when interrupted, if such is desired. Short of putting in the effort to observe the thread's interrupt status, I think you'll find that arbitrary tasks are quite resilient in the face of interrupts.
A task can check the thread's interrupt status at any time by calling Thread.isInterrupted()
. If you wish to make a runnable task interruptible, your code must occasionally check the interrupt status and exit the run()
method if the task has been interrupted. If you don't want your task to be interrupted, simply ignore the thread's interrupt status.
JDK methods that examine the thread's interrupt status are consistently declared to throw InterruptedException
, so they are easily identified. I/O calls w