Short version:
Is it a known best practice never to
use Thread.interrupt()?
No.
Can you provide
evidence why it is broken / buggie,
and should not be used for writing
robust multithreaded code?
The opposite is true: it is critical for multithreaded code.
See Listing 7.7 in Java Concurrency in Practice for an example.
Longer version:
Around here, we use this method in one specific place: handling InterruptedExceptions
. That may seem a little strange but here's what it looks like in code:
try {
// Some code that might throw an InterruptedException.
// Using sleep as an example
Thread.sleep(10000);
} catch (InterruptedException ie) {
System.err.println("Interrupted in our long run. Stopping.");
Thread.currentThread().interrupt();
}
This does two things for us:
- It avoids eating the interrupt. The classic textbook code always shows you a print or log message that makes note of the interrupt and then, effectively, ignores it.
- It passes the interrupt along without forcing a checked exception on this method.
When you receive an InterruptedException
, someone is trying to stop the application or thread. If you don't respond to that request, you have effectively created an unkillable zombie thread.