aspose file tools*
The moose likes Threads and Synchronization and the fly likes How to check wether object is currently locked Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to check wether object is currently locked" Watch "How to check wether object is currently locked" New topic
Author

How to check wether object is currently locked

nikhil jai
Greenhorn

Joined: Aug 07, 2008
Posts: 9

When we put synchronized on some object it acquires intrinsic lock associated wit that object . Similarly when we put synchronized on some function it will acquire the intrinsic lock on "this" object .

Now my problem is there anyway to know whether the object is already locked or not .

For ex , say i have a class

class SyncExample{
synchronized void fun(){};
synchronized void fun2(){};
}
I call fun() using object of SyncExample class . Now before calling fun2() using same object ,I want to check whether the object is already locked , How can I do that ?




Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1823
    
    7

You can't using the intrinsic locking approach. If you need this level of control you'd have to implement your own custom lock class or use one of the implementations provided by the java.util.concurrent.locks package.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
nikhil jai
Greenhorn

Joined: Aug 07, 2008
Posts: 9
Hi Jelle . Thanks for you reply . Ya i know that can be implemented by using custom lock . But actually m working on code which has already been implemented and there is a deadlock in the code which is coming very rarely but still it can come .
The code uses synchronized functions for making them thread safe . There is cyclic dependency for 2 objects , each of which have been locked by two threads using synchronized .
So I was looking for rollback solution in case deadlock happens . But for that I need some mechanism to know there is deadlock . So if i know that other object is already locked I can release the lock on which I acquired the lock , let the other thread finish and then again try for the resources .
Can you suggest anything on this ?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

First thing I would try is modifying how you do locking.

1) Don't lock on 2 different objects. This is asking for a deadlock situation. If you need to protect two different sets of data, provide one data lock which you use to protect both sets of data.

2) Shorten the amount of code you have in the synchronized blocks. You can prevent deadlocks by making each synchronized block short, copying the protected data to local variables to work on.


If I make the two small synch blocks for copying data from and to main memory then I can help prevent deadlock by making sure no Thread tries to hold 2 locks at the same time.

3) Do what Jelle said previously. Substitute all your intrinsic locks with high level locks, which at least provides tryLock() methods. You should probably do that anyway when you can (I wouldn't actively search out all your code which uses synchronized blocks or methods and replace them, but if I am working on code anyway I do tend to replace synchronized blocks with locks around the java.util.concurrenent.locks.


Steve
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Hi Nikhil,

Also, there is one more administrative matter I would like to bring to your attention. Could you please check your PMs?

Thanks,
Steve
 
Consider Paul's rocket mass heater.
 
subject: How to check wether object is currently locked