Hacker News new | ask | show | jobs
by anameaname 2971 days ago
As another surprising thing, calling interrupted() actually clears the interrupted bit. If it's true, you pretty much have to throw InterruptedException. Otherwise, you have to re set the bit, and exit quietly.
1 comments

> calling isInterrupted() actually clears the interrupted bit.

Thats incorrect. Calling Thread#interrupted[0] clears the interrupt status and returns true if the current thread was interrupted. Thread#isInterrupted()[1] only returns the value of the interrupt status on the thread receiving the method call. The interrupt status is unaffected by that method call.

[0] https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.h...

[1] https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.h...

You are right, editted.
As the article itself says, you're unlikely to write correct code if you use these language features. :)