what happens when a thread is interrupted while blocking on a wait()?
Asked Answered
P

2

6

Considering the fact that wait() can only be called in a synchronized context which subsequently release the monitor until a notify/nofityAll has been called on the same object by another thread,

Assume Thread A is blocking on a wait() which results in Thread B acquiring the lock. Now if we interrupt Thread A, would control be transferred immediately to Thread A ? in which case, since the try catch block handling the InterrupException is within the synchronized context, and since only one Thread can hold the monitor at a time, what will happen to Thread B ? should it move to a blocked state until Thread A has finished execution ?

Thanks in Advance

Pamper answered 9/11, 2011 at 20:21 Comment(0)
P
9

Reading the documentation does in fact help:

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#wait%28long%29

Thus, when the thread is interrupted, it has to re-acquire the Object's monitor to restore the synchronisation state before the exception is thrown. The same holds for returning from the wait(long) call after the specified amount of time has elapsed.

The thread T is then removed from the wait set for this object and re-enabled for thread scheduling. It then competes in the usual manner with other threads for the right to synchronize on the object; once it has gained control of the object, all its synchronization claims on the object are restored to the status quo ante - that is, to the situation as of the time that the wait method was invoked. Thread T then returns from the invocation of the wait method. Thus, on return from the wait method, the synchronization state of the object and of thread T is exactly as it was when the wait method was invoked.

If the current thread is interrupted by another thread while it is waiting, then an InterruptedException is thrown. This exception is not thrown until the lock status of this object has been restored as described above.

Particularize answered 9/11, 2011 at 20:29 Comment(4)
+1. I searched in the doc for wait() (without argument), and it was not mentioned (and there was no link to the overloaded wait method). Too bad.Rolanda
ah! i dont see the explanation in the java 6 doc bit.ly/tNJ1VT. anyway, in that case I guess Thread A MUST recheck the condition predicate on receiving the Exception before proceeding any furtherPamper
@arun_suresh: As I said, it's in the wait(long) javadoc: download.oracle.com/javase/6/docs/api/java/lang/…. An interrupt is normally use to make a thread stop. So you usually don't proceed any further anyway.Rolanda
@JBNizet :that means when a thread A is interrupted in waiting state( for wait with no args) then it will get be out of wait and will start contending for the lock as before and the moment it gets lock , it will start from the same point as it was left at the moment was interrupted. So does it also mean the other thread(Thread B) which was holding the lock just before interruption in wait will regain the lock ?Karp
S
2

I believe that A will become runnable but will wait until it can acquire the lock before proceeding with the catch clause. It won't force B into a blocked state. The whole point of a synchronized block is that the thread holding the lock is guaranteed that no other thread can synchronize on the same lock until it gives up its lock; forcing B into a blocked state and letting A reacquire the lock would violate the very essence of synchronization.

Stockist answered 9/11, 2011 at 20:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.