`在java中监视每个线程的cpu使用情况?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/755899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:06:05  来源:igfitidea点击:

`Monitor cpu usage per thread in java?

javamultithreadingcpu-usage

提问by awk

I would like to ask whether there is some simple way to determine cpu usageper thread in java. Thanks

我想问一下是否有一些简单的方法来确定cpu usage每个线程java。谢谢

回答by Gadi

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() .

虽然这取决于平台,但我相信您正在寻找的是 ThreadMXBean:http: //java.sun.com/j2se/1.5.0/docs/api/java/lang/management/ThreadMXBean.html。例如,您可以使用 getThreadUserTime 方法来获取所需内容。要检查您的平台是否支持 CPU 测量,您可以调用 isThreadCpuTimeSupported() 。

回答by VonC

I believe the JConsole(archived link) does provide this kind of information through a plugin

我相信JConsole存档链接)确实通过插件提供了这种信息

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

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

It uses ThreadMXBeangetThreadCpuTime() function.

它使用ThreadMXBeangetThreadCpuTime() 函数。

Something along the line of:

沿线的东西:

        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);
            }
        }

回答by dfa

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

通过使用 java.lang.management.ThreadMXBean。如何获取 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.

希望能帮助到你。

回答by idrosid

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

事实上,对象 ThreadMXBean 提供了您需要的功能(但它可能不会在所有虚拟机上实现)。

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

在 JDK 1.5 中,有一个演示程序可以完全满足您的需求。它在文件夹 demo/management 中,名为 JTop.java

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

不幸的是,它在 Java6 中不存在。也许您可以在 google 上找到或下载 JDK5。

回答by peter

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

试试“TopThreads”JConsole 插件。见http://lsd.luminis.nl/top-threads-plugin-for-jconsole/

回答by Kanagavelu Sugumar

Option_1: Code level

Option_1:代码级别

In your business logic code; in the beginning call start() API and in the finally block call stop(). So that you will get CPU time for executing your logic by the current running thread. Then log it. Reference.

在您的业务逻辑代码中;在开始调用 start() API 和在 finally 块中调用 stop()。这样您就可以获得 CPU 时间来通过当前正在运行的线程执行您的逻辑。然后记录一下。参考

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 using plugins (AIX IBM box which don't have jvisualvm support)

Option_2:使用插件监控级别(不支持 jvisualvm 的 AIX IBM box)

If you think it is delay in adding code now, then you can prefer JConsole with plugins support. I followed thisarticle. Download the topthreads jar from that article and run ./jconsole -pluginpath topthreads-1.1.jar

如果您认为现在添加代码会延迟,那么您可以更喜欢带有插件支持的 JConsole。我关注了这篇文章。从那篇文章下载 topthreads jar 并运行./jconsole -pluginpath topthreads-1.1.jar

Option_3: Monitor level using TOP (shift H) + JSTACK (Unix machine which has 'Shif+H' support)

Option_3:使用 TOP (shift H) + JSTACK(支持 'Shif+H' 的 Unix 机器)监视级别

Follow thistutorial, where top command will give option to find top CPU thread (nid). Take that check that nid in jstack output file.

按照教程,其中 top 命令将提供查找顶级 CPU 线程 (nid) 的选项。检查 jstack 输出文件中的 nid。