Thread.interrupt() in Java: what's the point? [duplicate] Thread.interrupt() in Java: what's the point? [duplicate] multithreading multithreading

Thread.interrupt() in Java: what's the point? [duplicate]


From a related post:

Thread.interrupt() sets the interrupted status/flag of the target thread. Then code running in that target thread MAY poll the interrupted status and handle it appropriately. Some methods that block such as Object.wait() may consume the interrupted status immediately and throw an appropriate exception (usually InterruptedException)

From javadocs

Many methods that throw InterruptedException, such as sleep, are designed to cancel their current operation and return immediately when an interrupt is received.

By convention, any method that exits by throwing an InterruptedException clears interrupt status when it does so. However, it's always possible that interrupt status will immediately be set again, by another thread invoking interrupt.

So it provides more functionality rather than simply setting a boolean flag.