File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to check wether object is currently locked

 
nikhil jai
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nikhil jai
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic