• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to check wether object is currently locked

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?




 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic