Reputation: 83
I was going through oracle java tutorial about threads and I saw this example
src: http://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html
What if a thread goes a long time without invoking a method that throws InterruptedException? Then it must periodically invoke Thread.interrupted, which returns true if an interrupt has been received. For example:
for (int i = 0; i < inputs.length; i++) {
heavyCrunch(inputs[i]);
if (Thread.interrupted()) {
// We've been interrupted: no more crunching.
return;
}
The tutorial adds : The interrupt mechanism is implemented using an internal flag known as the interrupt status. Invoking Thread.interrupt sets this flag. When a thread checks for an interrupt by invoking the static method Thread.interrupted, interrupt status is cleared. The non-static isInterrupted method, which is used by one thread to query the interrupt status of another, does not change the interrupt status flag.
By convention, any method that exits by throwing an InterruptedException clears interrupt status when it does so. However, it's always possible that interrupt status will immediately be set again, by another thread invoking interrupt.
now I'm confused. When does it happen? if there is an interrupt, shouldn't the thread receive an InterruptedException?
When someone might use these method of checking in their code? The Thread.interrupt (the flag) is static, so with the above check, we actually check if any of the treads has been interrupted? even if that is the case, according to the tutorial, When a thread checks for an interrupt by invoking the static method Thread.interrupted, interrupt status is cleared. or any method that exits by throwing an InterruptedException clears interrupt status when it does so
So, Am I missing something? or this is only useful, if some thread has received an interrupt, but doesn't check it, or does not throw it?
Upvotes: 0
Views: 5472
Reputation: 831
Many times a Thread is doing a lot of waiting or blocking. In almost all of these cases the method that is doing the blocking supports InterruptedException. Some examples:
What the referenced article is pointing out is that if your thread is not blocking, meaning there is no method that supports InterruptedException, then it is your responsibility to check if your thread has been interrupted. This is accomplished by testing Thread.interrupted()
.
The method Thread.interrupt()
is in fact not static and is called from another Thread to interrupt that thread. See the following:
Does that make sense?
Upvotes: 1
Reputation: 136112
This is Thread.interrupted implementation
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
private native boolean isInterrupted(boolean ClearInterrupted);
as you can see it can check / reset only current thread's interrupted status. If it were not static you could call it on other threads and reset their status which is not allowed by API.
Upvotes: 5
Reputation: 16294
Not all methods, in Thread
, throws InterrupedException
s. It is mostly common at sleeps and waits, or any time consuming methods. This is the way Java implemented it. (sleep(...)
is native
code, so I cant show you the source.)
For example:
try {
Thread.sleep(1000); // one sec
} catch(InterruptedException e){
}
But you can't force to include a simple for
loop, in a try..catch
clause.
If you subclass Thread
, you should follow the conventions, posted.
For your second question, about the static
method, here's the internal code:
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
So you can see, it just sets, an instance in the current thread.
Upvotes: 2