Java Windows cpu 使用率_请问Java中获得CPU使用率的办法?windows系统

展开全部

1、确定当前系统安装的jdk是1.6版本以上

2、windows系统中有获取cpu使用率的可执行文636f70793231313335323631343130323136353331333337626230件exe,只要在java中获取该文件的执行路径,通过Java调用即可。

3、获取操作系统可执行文件目录procCmd

4、调用java的Runtime.getRuntime().exec执行cmd应用程序

5、利用java中sleep来计算睡眠前后cpu的忙碌时间与空闲时间,因为sleep不会释放系统资源

6、根据忙碌时间占总时间的比例来计算cpu使用率

示例:private double getCpuRatioForWindows() {

try {

String procCmd = System.getenv("windir")

+ "//system32//wbem//wmic.exe process get Caption,CommandLine,"

+ "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";

// 取进程信息

long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));

Thread.sleep(CPUTIME);

long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));

if (c0 != null && c1 != null) {

long idletime = c1[0] - c0[0];

long busytime = c1[1] - c0[1];

return Double.valueOf(

PERCENT * (busytime) / (busytime + idletime))

.doubleValue();

} else {

return 0.0;

}

} catch (Exception ex) {

ex.printStackTrace();

return 0.0;

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值