wood burning stoves*
The moose likes Beginning Java and the fly likes Question about object lock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about object lock" Watch "Question about object lock" New topic
Author

Question about object lock

George Lin
Ranch Hand

Joined: Jan 11, 2005
Posts: 125
Hello everyone,


I want to know which method owns the lock of a specific object (some of my methods are blocked because they can not achieve the lock of a specific object, and I am wondering which method owns the lock to solve the deadlock issue). Does anyone know how to get the information?


Thanks in advance,
George
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
There are probably fancy ways, but I would put these statements like these at the beginning and end of each method or synchronized block:
System.out.println( "method3 requests lock " + this );
System.out.println( "method3 holds lock " + this );
System.out.println( "method3 releases lock " + this );

If you need to know which this is which instance, add an informative println with this to the constructor:
System.out.println( "Salary object for employee " + empNo + " is " + this );


If there are many lines of output, write to a file and examine the end of the file for the deadlock. If you do this, be sure to use a PrintWriter or PrintStream with autoflush true in the constructor or use flush() after every write so you get the last lines of output.
[ February 01, 2005: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
George Lin
Ranch Hand

Joined: Jan 11, 2005
Posts: 125
Thanks Mike,


Originally posted by Mike Gershman:
There are probably fancy ways, but I would put these statements like these at the beginning and end of each method or synchronized block:
System.out.println( "method3 requests lock " + this );
System.out.println( "method3 holds lock " + this );
System.out.println( "method3 releases lock " + this );

If you need to know which this is which instance, add an informative println with this to the constructor:
System.out.println( "Salary object for employee " + empNo + " is " + this );


If there are many lines of output, write to a file and examine the end of the file for the deadlock. If you do this, be sure to use a PrintWriter or PrintStream with autoflush true in the constructor or use flush() after every write so you get the last lines of output.

[ February 01, 2005: Message edited by: Mike Gershman ]


Your reply is very helpful. I am wondering how to write a utility class so that I need not hardcode all "System.out" manually. I want this utility class to automatically track/record the retrieval and release of locks. Do you have any good ideas?


regards,
George
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
It is possible that jconsole (Java 1.5) could help you. (I have not used it for this purpose.)

There is also a staic method of the Thread class called holdsLock which you can insert into your methods. It takes an Object as an argument and it will tell you whether the current thread holds the lock on the object.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
George Lin
Ranch Hand

Joined: Jan 11, 2005
Posts: 125
Thanks Barry,


Originally posted by Barry Gaunt:
It is possible that jconsole (Java 1.5) could help you. (I have not used it for this purpose.)

There is also a staic method of the Thread class called holdsLock which you can insert into your methods. It takes an Object as an argument and it will tell you whether the current thread holds the lock on the object.


The information you provided are very useful. I am wondering whether there exists a solution for JDK 1.3 platform. I am using JDK 1.3 platform.


regards,
George
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about object lock
 
Similar Threads
Does each instance of the Object own a lock OR .....
synchronization construct
thread
Locking trouble...
How to lock a specific instance variable