From 0501dba5bc2e5f9652918e6fa27213749208fc31 Mon Sep 17 00:00:00 2001 From: xyli Date: Fri, 3 Mar 2023 13:54:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Ecpu=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/TestCpu.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/test/java/TestCpu.java b/src/test/java/TestCpu.java index d06531b..f06b144 100644 --- a/src/test/java/TestCpu.java +++ b/src/test/java/TestCpu.java @@ -28,6 +28,23 @@ public class TestCpu { return "内存已使用:" + compare.intValue() + "%"; } + public static String getCpuRatio() { + try { + String procCmd = "wmic process get Caption,CommandLine,KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount"; + long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd)); // 取进程信息 + 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 "CPU使用率:" + Double.valueOf(100 * (busytime) * 1.0 / (busytime + idletime)).intValue() + "%"; + } else { + return "CPU使用率:" + 0 + "%"; + } + } catch (Exception ex) { + ex.printStackTrace(); + return "CPU使用率:" + 0 + "%"; + } + } -- Gitee