Utils.java 文件源码

java
阅读 24 收藏 0 点赞 0 评论 0

项目:Shadbot 作者:
public static double getProcessCpuLoad() {
    double cpuLoad;
    try {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem");
        AttributeList list = mbs.getAttributes(name, new String[] { "ProcessCpuLoad" });

        if(list.isEmpty()) {
            return Double.NaN;
        }

        Attribute att = (Attribute) list.get(0);
        Double value = (Double) att.getValue();

        if(value == -1.0) {
            return Double.NaN;
        }

        cpuLoad = value * 100d;
    } catch (InstanceNotFoundException | ReflectionException | MalformedObjectNameException err) {
        cpuLoad = Double.NaN;
    }

    return cpuLoad;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号