aspose file tools*
The moose likes Threads and Synchronization and the fly likes Should lock be reacquired to enter catch block? [InterruptedException] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Should lock be reacquired to enter catch block? [InterruptedException]" Watch "Should lock be reacquired to enter catch block? [InterruptedException]" New topic
Author

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

Prabakar Kalivaradan
Greenhorn

Joined: Dec 12, 2011
Posts: 20

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?



-prabak
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
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

Joined: Dec 12, 2011
Posts: 20

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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Should lock be reacquired to enter catch block? [InterruptedException]