tags:

views:

2164

answers:

5

Hi, I would like to ask whether there is some simple way to determine cpu usage per thread in java. Thanks

+1  A: 

Though this is platform dependent, I believe what you're looking for is the ThreadMXBean: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/management/ThreadMXBean.html . You can use the getThreadUserTime method, for example, to get what you need. To check if your platform supports CPU measurement, you can call isThreadCpuTimeSupported() .

Gadi
+3  A: 
VonC
A: 

by using java.lang.management.ThreadMXBean. How to obtain a ThreadMXBean:

 ThreadMXBean tmxb = ManagementFactory.getThreadMXBean();

then you can query how much a specific thread is consuming by using:

 long cpuTime = tmxb.getThreadCpuTime(aThreadID);

Hope it helps.

dfa
A: 

Indeed the object ThreadMXBean provides the functionality you need (however it might not be implemented on all virtual machines).

In JDK 1.5 there was a demo program doing exactly what you need. It was in the folder demo/management and it was called JTop.java

Unfortnately, it's not there in Java6. Maybe you can find at with google or download JDK5.

idrosid
A: 

Hi,

Interresting post. Here is where recover this JTop.java : http://rejeev.googlepages.com/JTop.jar

I've successfully installed and used it. However, I do not manage to retrieve the same percentage values from my own ThreadMXBean bean instance : => one difference that I can see is that my .java program is running "locally" (together with the threads to monitor, on the same JVM instance) whereas Jconsole is run "remotely" (on its own JVM instance).

But in my case, unfortunately, I have to use the local manner : => would you be aware about any problem when monitoring our Java process locally ?

Any help being very appreciated,

Regards.