I'm trying to find out how much time is spent waiting for locks in a
program. Supposedly, there's a way to do this using the built-in
profiler in Sun's JVM:
java -Xrunhprof:monitor=y
which should write a log file with the following section:
MONITOR TIME is a profile of monitor contention obtained by measuring
the time spent by a thread waiting to enter a monitor.
Entries in this record are TRACEs ranked by the percentage
of total monitor contention time and a brief description
of the monitor. The "count" field indicates the number of
times the monitor was contended at that TRACE.
However, when I try running this on a sample program that has to wait
5 seconds for a lock, this section is always empty! I tried using
Sun's 1.4.1_02 SDK as well as IBM's 1.3.1 SDK, on Windows XP
Professional.
Why doesn't this work? Does it work for anybody?
-Jeff
Here's the program I'm using to test it:
package foo;
import java.util.*;
public class Foo {
public static void main(String[] args) throws Exception {
Date start = new Date();
Foo foo = new Foo();
Thread thread1 = new Thread(new MyThread(foo), "thread 1");
Thread thread2 = new Thread(new MyThread(foo), "thread 2");
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.err.println("Done!");
Date finish = new Date();
System.err.println("time = " + (finish.getTime() - start.getTime()));
}
synchronized void synched() throws Exception {
System.err.println(
"Got lock, thread = " + Thread.currentThread().getName());
Thread.sleep(5000);
}
static class MyThread implements Runnable {
Foo foo;
public MyThread(Foo foo) {
this.foo = foo;
}
public void run() {
try {
System.err.println(
"Acquiring lock, thread = "
+ Thread.currentThread().getName());
foo.synched();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Stephen Kellett - 27 Nov 2003 20:24 GMT
>I'm trying to find out how much time is spent waiting for locks in a
>program.
http://www.softwareverify.com
Java Thread Validator
Currently in beta test. Windows NT/2K/XP platforms.
Stephen

Signature
Stephen Kellett
Object Media Limited http://www.objmedia.demon.co.uk
RSI Information: http://www.objmedia.demon.co.uk/rsi.html