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
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.
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.
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.