views:

1114

answers:

8

Java question: As far as I know, there are two ways to check inside a thread whether the thread received an interrupt signal, Thread.interrupted() and Thread.isInterrupted(), and the only difference between them is that the former resets the internal interrupted flag.

So far, I've always used Thread.isInterrupted() and never had any problems with it. Then again, most tutorials I've seen recommend using Thread.interrupted(). Is there any specific reason for that?

+10  A: 

interrupted() is static and checks the current thread. isInterrupted() is an instance method which checks the Thread object that it is called on.

A common error is to call a static method on an instance.

Thread myThread = ...;
if (myThread.interrupted()) {} // WRONG! This might not be checking myThread.
if (myThread.isInterrupted()) {} // Right!

Another difference is that interrupted() also clears the status of the current thread. In other words, if you call it twice in a row and the thread is not interrupted between the two calls, the second call will return false even if the first call returned true.

The Javadocs tell you important things like this; use them often!

Michael Myers
Then it's Thread.currentThread().isInterrupted() vs. Thread.interrupted();So the first one is used for checking threads other than the current one?
python dude
Yep, that would be it. Also there's the bit about clearing the status, which I've added to the answer.
Michael Myers
+7  A: 

If you use interrupted, what you're asking is "Have I been interrupted since the last time I asked?"

isInterrupted tells you whether the thread you call it on is currently interrupted.

Anon.
nice answer, very easy understanding.
Warrior
+1  A: 

Thread interruption in Java is advisory. If you call Thread.interrupt() then it will set the flag and cancel any outstanding IO tasks (which will throw InterruptedException). However it is up to code that is executing in the thread to handle this. Doing so is called implementing the Thread interruption policy.

However because Thread's interrupted state is shared it is important that any such handling be Thread Safe. You don't want some other thread going off and trying to do something with the interrupted flag if you are handling it. For this reason the Thread.interrupted() flag makes this atomic so it is used when you want to say: "If this thread was interrupted then I am going to deal with it). Usually this will involve cleaning up some resources. Once you are done you should probably propogate the interrupted flag so that callers can handle it. You can do this by recalling Thread.interrupt.

Dean Povey
@Dean, typo on the exception name: it's just InterruptedException.
Bob Cross
Thanks, was too lazy to look it up.
Dean Povey
+1  A: 

The interrupted() method is a class method that always checks the current thread and clears the interruption "flag". In other words, a second call to interrupted() will return false.

The isInterrupted() method is an instance method; it reports the status of the thread on which it is invoked. Also, it does not clear the interruption flag. If the flag is set, it will remain set after calling this method.

erickson
+1  A: 

Here are a couple of examples of how you might use these methods:

  1. If you were writing your own thread pool, you might want to check the interrupted status on one of the threads that you are managing. In that case, you would call managedThread.isInterrupted() to check it's interrupted status.

  2. If you are writing your own InterruptedException handlers that don't immediately retrigger an equivalent exception via Thread.currentThread().interrupt() (for example, you might have a finally block after your exception handlers), you might want to check whether that thread that you are currently running on has been interrupted via an outside call or InterruptedException. In that case, you would check the boolean value of Thread.interrupted() to check on the status of your current thread.

The second method is really only ever useful to me in situations where I'm afraid that someone has written an exception eater at a lower level that, by extension, has eaten an InterruptedException as well.

Bob Cross
A: 

I would think it is better practice, that if you want to react to interrupting, to better use Thread.currentThread().interrupted(). Now when using it of course you need to understand its semantics. When invoking interrupted() you should promptly clean up any book keeping in the thread and exit gracefully (assuming that you responding to an interruption acts similar to how other concurrency constructs react to interruption).

You will see alot of concurrency constructs notice Thread.currentThread().interrupted evaluate to true and throw new InterruptedException()

John V.
+1  A: 

This reference explains the whole story fairly well: http://www.ibm.com/developerworks/java/library/j-jtp05236.html

Jack Parker
A: 

interrupted() method is a static method of class thread checks the current thread and clear the interruption "flag".i.e. a second call to interrupted() will return false.

isInterrupted() method is an instance method; it reports the status of the thread on which it is invoked. it does not clear the interruption flag.

If the flag is set, it will remain set after calling this method.

Thread myThread = ...; if (myThread.interrupted()) {} //error Thread.interrupted()//right if (myThread.isInterrupted()) {} // Right

Vijay Bhaskar Semwal