问题
I have a thread which has an incoming job queue (a LinkedList
containing job descriptions). The thread blocks with wait()
on the queue when there's no job to work on. An external job dispatcher object awakes it with notify()
when it places new jobs on the queue.
At shutdown of my program i call interrupt()
on the Thread. This raises InterruptedException
when the Thread awaits for jobs in wait()
. My question is: what will happen if i interrupt the Thread while it's not blocking but doing some kind of work, the processed item was the last in the queue (so queue is now empty) and execution pasts the isInterrupted()
check before the interrupted flag is set so it calls wait()
again? Will it throw an InterruptedException
because the interrupted flag has already been set or the thread waits forever because new jobs will never arrive to the queue and there's no one to interrupt the wait?
回答1:
yes, your interrupted thread will throw an InterruptedException upon calling wait(). this is pretty simple to test for yourself.
public class TestInt {
public static void main(String[] args) throws Exception
{
Thread.currentThread().interrupt();
synchronized(TestInt.class) {
TestInt.class.wait();
}
}
}
also, note the javaodc for Object.wait()
:
InterruptedException - if any thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.
回答2:
Read the API specifications for Thread.interrupt() -
Interrupts this thread.
Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.
If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.
If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException.
If this thread is blocked in a Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.
If none of the previous conditions hold then this thread's interrupt status will be set.
Interrupting a thread that is not alive need not have any effect.
So, all that will happen is the thread's interrupt status will be set.
EDIT
If you read the API specifications for Object.wait(), you'll see the following -
Throws:
InterruptedException - if any thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.
来源:https://stackoverflow.com/questions/6698977/thread-interrupt-will-it-cancel-oncoming-wait-call