Why does Thread.isInterrupted () always return false?

This behaviour is typically documented in methods that throw that exception. For example, the javadoc for Object.wait() says:

InterruptedException – if any thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.

Indeed, the javadoc for the exception itself says this:

“Occasionally a method may wish to test whether the current thread has been interrupted, and if so, to immediately throw this exception. The following code can be used to achieve this effect:

if (Thread.interrupted())  // Clears interrupted status!
     throw new InterruptedException();

Note how they emphasize that the flag should be cleared before the exception is thrown.


Why was it designed to work like this? You’d have to ask the designers, but I expect they figured that an exception handler should handle the situation, and that there should therefore be no need for the flag to still be set at that point. (If the handler doesn’t fully handle the situation it can either rethrow the exception, or call Thread.getCurrentThread.interrupt() to set the flag again.)

Leave a Comment