• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about object lock

 
George Lin
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
George Lin
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
George Lin
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic