Monitoring CPU usage in a thread in java?

I would like to ask if there is an easy way to determine processor usage in a thread in java. Thanks

+28
java
Apr 16 '09 at 12:27
source share
6 answers

I believe JConsole ( archived link ) provides such information through a plugin

JConsole Thread CPU usage, after blogs.oracle.com/lmalventosa/resource/thread_cpu_usage.jpg

It uses the ThreadMXBean function getThreadCpuTime ().

Something along the line:

long upTime = runtimeProxy.getUptime(); List<Long> threadCpuTime = new ArrayList<Long>(); for (int i = 0; i < threadIds.size(); i++) { long threadId = threadIds.get(i); if (threadId != -1) { threadCpuTime.add(threadProxy.getThreadCpuTime(threadId)); } else { threadCpuTime.add(0L); } } int nCPUs = osProxy.getAvailableProcessors(); List<Float> cpuUsageList = new ArrayList<Float>(); if (prevUpTime > 0L && upTime > prevUpTime) { // elapsedTime is in ms long elapsedTime = upTime - prevUpTime; for (int i = 0; i < threadIds.size(); i++) { // elapsedCpu is in ns long elapsedCpu = threadCpuTime.get(i) - prevThreadCpuTime.get(i); // cpuUsage could go higher than 100% because elapsedTime // and elapsedCpu are not fetched simultaneously. Limit to // 99% to avoid Chart showing a scale from 0% to 200%. float cpuUsage = Math.min(99F, elapsedCpu / (elapsedTime * 1000000F * nCPUs)); cpuUsageList.add(cpuUsage); } } 
+17
Apr 16 '09 at 12:40
source share

using java.lang.management.ThreadMXBean . How to get ThreadMXBean:

  ThreadMXBean tmxb = ManagementFactory.getThreadMXBean(); 

then you can query how much specific thread it consumes using:

  long cpuTime = tmxb.getThreadCpuTime(aThreadID); 

Hope this helps.

+11
Apr 16 '09 at 12:41
source share

Option_1: code level

In the business logic code; at the beginning of the start () API call and at the end of the stop () call block. So you get processor time to execute your logic on the current thread. Then write it down. Link

 class CPUTimer { private long _startTime = 0l; public void start () { _startTime = getCpuTimeInMillis(); } public long stop () { long result = (getCpuTimeInMillis() - _startTime); _startTime = 0l; return result; } public boolean isRunning () { return _startTime != 0l; } /** thread CPU time in milliseconds. */ private long getCpuTimeInMillis () { ThreadMXBean bean = ManagementFactory.getThreadMXBean(); return bean.isCurrentThreadCpuTimeSupported() ? bean.getCurrentThreadCpuTime()/1000000: 0L; } } 

Option_2: monitor level with plugins (IBM AIX box that does not have jvisualvm support)

If you think this is a delay with adding code now, you might prefer JConsole with plugin support. I followed this article. Download the jar of topthreads from this article and run ./jconsole -pluginpath topthreads-1.1.jar

Option_3: monitor level using TOP (shift H) + JSTACK (Unix machine supporting Shif + H)

Follow this tutorial where the top command will give you the opportunity to find the top CPU thread (nid). Take that check that nid in the jstack output file.

+3
Feb 09 '15 at 11:43
source share

Try connecting to the JConsole "TopThreads" plugin. See http://lsd.luminis.nl/top-threads-plugin-for-jconsole/

+2
Jan 25 2018-11-21T00:
source share

Although it depends on the platform, I believe you are looking for ThreadMXBean: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/management/ThreadMXBean.html . For example, you can use the getThreadUserTime method to get what you need. To check if your platform supports CPU measurement, you can call isThreadCpuTimeSupported ().

+1
Apr 16 '09 at 12:39
source share

In fact, the ThreadMXBean object provides the functions you need (however, it cannot be implemented on all virtual machines).

JDK 1.5 had a demo program that did exactly what you needed. This was in the demo / management folder, and it was called JTop.java

Unfortunately, it is not in Java6. Perhaps you can search on Google or download JDK5.

+1
Apr 16 '09 at 12:49
source share



All Articles