0

The class java.lang.ThreadInfo provides some very useful methods which provide statistic concerning synchronization in Java. For instance:

getBlockedTime() Returns the approximate accumulated elapsed time (in milliseconds) that the thread associated with this ThreadInfo has blocked to enter or reenter a monitor since thread contention monitoring is enabled. [...]

getWaitedCount() Returns the total number of times that the thread associated with this ThreadInfo waited for notification. [...]

I cannot find functions to access these information from the JVMTI API. Do I overlook it or do I have to collect these information myself?


Besides from synchronization statistic, it seems you cannot even get a Threads id, which can be obtained by using Thread.currentThread().getId();

Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143

1 Answers1

1

It's not quite as straightforward, but I think you can arrive at the blocked time and wait count via JVMTI events (see the events to do with "monitors": "Monitor Contended Enter" and "Monitor Wait" etc).

Re the thread IDs, no it doesn't look like JVMTI assigns these. On the other hand, I'm not sure that the IDs given in ThreadInfo actually refer to any "real" ID assigned by the O/S.

Neil Coffey
  • 21,615
  • 7
  • 62
  • 83
  • @Neil-Coffey Yes, I guess I have to collect it myself. Yes, it as I look at the numbers, the threads are identified only uniquely inside one JVM. However this is then another thing I need to keep track of in order to distinguish different threads. – Konrad Reiche Apr 17 '11 at 08:14
  • SetTag might be a simple option to keep track of the threads myself: http://download.oracle.com/javase/1.5.0/docs/guide/jvmti/jvmti.html#SetTag – Konrad Reiche Apr 17 '11 at 08:32
  • Yes I think you could use SetTag. Just do a quick test to make sure it's actually available on your VM before you do lots of coding. – Neil Coffey Apr 17 '11 at 13:58
  • P.S. If it's JUST for the purpose of telling threads apart, then you might want to check if the jthread pointers passed to you by the JVM are stable (so you can tell a thread is "the same as last time" because it has the same pointer, or different from thread B because the pointers are different). I can't honestly remember if this is the case though, and it may vary from VM to VM. – Neil Coffey Apr 18 '11 at 03:08