• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Should lock be reacquired to enter catch block? [InterruptedException]

 
Prabakar Kalivaradan
Greenhorn
Posts: 20
Chrome Eclipse IDE Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The situation is, a thread is hanging on code line with wait() and currently blocked. Another thread is holding the lock and sends interrupt signal to the first thread. Now, the first thread since it is in blocked state and doesn't have lock, has been interrupted and so it has received InterruptedException and need to handle it in catch block. Unfortunately the try-catch block also is inside synchronized block and hence need to reacquire the lock in order to enter the catch block.

Can someone explain what is the rule(JVM rules) in this scenario for the interrupted thread to reacquire the lock just to handle the InterruptedException? If lock is not available, will it stay blocked without actually receiving the InterruptedException?

In my experiment, I saw that within the catch block if a thread sleeps and receives no other interrupt signal, it kind of hogs the lock to itself and no other thread is able to enter the synchronized block.

Is this applicable to all kinds of Exceptions(not just InterruptedException)? Does an Exception receiving thread has higher priority in acquiring the lock? Can it further go on after catch block and execute the remaining statements within synchronized block since it acquired lock to handle the exception?

 
Mike Simmons
Ranch Hand
Posts: 3041
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabakar Kalivaradan wrote:Unfortunately the try-catch block also is inside synchronized block and hence need to reacquire the lock in order to enter the catch block.

Actually it doesn't matter where the catch block is. The thread cannot leave the wait() statement, in any way, until it reacquires the lock.

So your comments should be more like this:


Prabakar Kalivaradan wrote:
Can someone explain what is the rule(JVM rules) in this scenario for the interrupted thread to reacquire the lock just to handle the InterruptedException? If lock is not available, will it stay blocked without actually receiving the InterruptedException?

Yes, if the lock never becomes available, the thread will never leave the wait() method, period.

Prabakar Kalivaradan wrote:Is this applicable to all kinds of Exceptions(not just InterruptedException)?

Yes.

Prabakar Kalivaradan wrote:Does an Exception receiving thread has higher priority in acquiring the lock?

No, I don't think so.

Prabakar Kalivaradan wrote:Can it further go on after catch block and execute the remaining statements within synchronized block since it acquired lock to handle the exception?

Once it has reacquired the lock, it can go ahead, sure. It will retain that lock until it either hits another wait() method, or until it reaches the end of the sync block. Until one of those happens, there's no reason to think it will suddenly lose the lock again.
 
Prabakar Kalivaradan
Greenhorn
Posts: 20
Chrome Eclipse IDE Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, whatever is the condition of the thread, it has to honor the regular lock acquisition rules. I got it.

Thank you very much Mike

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic