Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread.interrupted() doesn't make sense  RSS feed

 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

The static method Thread.interrupted() doesn't make sense for me. Why on Earth would I test if the current thread has been interrupted.
I think it will always return false or it will never be reached.
The instance method isInterrupted() is more understandable.

What am I missing? Could you provide an example?

Thank you!
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The static interrupted method actually violates Principle of least astonishment. The name implies that the method tests whether the current thread has been interrupted but the main thing it does is clear the interrupted status of the current thread. The method name is just confusing here and unfortunately usually caused with bug.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leandro,

To address your question about the usefulness of the Thread.interrupted() method - I think you have a problem with it because it checks the current thread to see if it is interrupted. I guess you think there is a problem with that because if the Thread is interrupted how could it be executing to the point where it would be able to check if it was interrupted?

This all comes down to understanding what 'interrupting' a Thread is. Interrupting a Thread isn't causing it to stop execution, and it doesn't necessarily mean it throws an exception either. If you read the JavaDocs for Thread#interrupt() you will see that, except for a few specific situations, all that method does is set a status flag. You should think of it as a 'request to interrupt'. Your job, as a Thread-savvy programmer is to react to those requests when appropriate, and to respond to the interruption requests is to periodically check on the interrupted status of the Thread you are running in. There are two ways to do it - First you could get a reference to the current thread and check the isInterrupted() flag, and second you could call the static Thread.interrupted() method (as Muhammad said there is a difference in how the two methods function). Once you detect that an interrupt has been requested you can then react to it in a clean and orderly way.

As the JavaDocs state, certain code, like wait() and Thread.sleep() will check the interrupted status flag and react by throwing an exception, others will react in other ways. But if you have code that runs in its own Thread and has a continuous or long lived loop then it is often a good idea to make sure your code reacts to the interruption itself.

Muhammad, you had a different point about the side effect the Thread.interrupted() method has of resetting the interruption status. I agree that the method could be better named for self-documentation purposes but the fact that the method resets the flag is clearly stated in the method's documentation and so should not come as any surprise to a programmer who uses the method (after all, you do regularly read the JavaDocs don't you?)
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Muhammad! Yeah, interrupt definitely is not a good name. =)

Thanks a lot Steve! Now I'm more comfortable about this subject.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!