Java - Thread problem
Posted
by Yatendra Goel
on Stack Overflow
See other posts from Stack Overflow
or by Yatendra Goel
Published on 2010-05-03T07:15:30Z
Indexed on
2010/05/03
7:18 UTC
Read the original article
Hit count: 304
My question is related to all those methods(including Thread.sleep(...)
) which throw InterruptedException
.
I found a statement on Sun's tutorial saying
InterruptedException
is an exception thatsleep
throws 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)?
© Stack Overflow or respective owner