Search code examples
javamultithreadingtry-catchmonitorinterrupted-exception

If a thread gets interrupted on wait(), will the monitor be re-acquired before reaching the catch block outside of the synchronization block?


Please consider the following code:

try{
    synchronized(myLock){
        myLock.wait(); // gets interrupted while waiting.
   }
}catch(InterruptedException ie){

}

If a thread gets interrupted while blocked on wait(), will the monitor of myLock be re-acquired (temporarily?) before ended up in the catch block?


Solution

  • As the javadoc states

    If the current thread is interrupted by any thread before or 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.

    So, yes, it will be blocked waiting for the lock to be reacquired and then throw the exception.