My question is related to all those methods(including Thread.sleep(...)) which throw InterruptedException.
I found a statement on Sun’s tutorial saying
InterruptedExceptionis an exception thatsleepthrows when another thread interrupts the current thread while sleep is active.
Is that means that the interrupt will be ignored if the sleep is not active at the time of interrupt?
Suppose I have two threads: threadOne and threadTwo. threadOne creates and starts threadTwo. threadTwo executes a runnable whose run method is something like:
public void run() {
:
:
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
return;
}
:
:
: // In the middle of two sleep invocations
:
:
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
return;
}
:
:
}
After thread creation, threadOne interrupts threadTwo. Suppose the threadTwo is in the middle of two sleep invocations at the time of interrupt (when no sleep method was active), then will the second sleep method throw InterrupteException as soon as it is invoked?
If not, then will this interrupt will be ignored forever?
How to be sure that threadTwo will always know about the interrupt (doesn’t matter whether its one of the sleep method is active or not)?
On Sun’s Windows JDK, the thread will in fact throw
InterruptedExceptionwhen enteringsleep():The API documentation of
sleep()can be interpreted to mean that this is mandatory behaviour:But that’s not very clear, so I wouldn’t depend on it and instead check
isInterrupted()manually.