Java如何读取CPU的数据信息 java如何获取系统内存、cpu等信息。

\u8bf7\u95eeJava\u4e2d\u83b7\u5f97CPU\u4f7f\u7528\u7387\u7684\u529e\u6cd5\uff1fwindows\u7cfb\u7edf

1\u3001\u786e\u5b9a\u5f53\u524d\u7cfb\u7edf\u5b89\u88c5\u7684jdk\u662f1.6\u7248\u672c\u4ee5\u4e0a
2\u3001windows\u7cfb\u7edf\u4e2d\u6709\u83b7\u53d6cpu\u4f7f\u7528\u7387\u7684\u53ef\u6267\u884c\u6587\u4ef6exe\uff0c\u53ea\u8981\u5728java\u4e2d\u83b7\u53d6\u8be5\u6587\u4ef6\u7684\u6267\u884c\u8def\u5f84\uff0c\u901a\u8fc7Java\u8c03\u7528\u5373\u53ef\u3002
3\u3001\u83b7\u53d6\u64cd\u4f5c\u7cfb\u7edf\u53ef\u6267\u884c\u6587\u4ef6\u76ee\u5f55procCmd
4\u3001\u8c03\u7528java\u7684Runtime.getRuntime().exec\u6267\u884ccmd\u5e94\u7528\u7a0b\u5e8f
5\u3001\u5229\u7528java\u4e2dsleep\u6765\u8ba1\u7b97\u7761\u7720\u524d\u540ecpu\u7684\u5fd9\u788c\u65f6\u95f4\u4e0e\u7a7a\u95f2\u65f6\u95f4\uff0c\u56e0\u4e3asleep\u4e0d\u4f1a\u91ca\u653e\u7cfb\u7edf\u8d44\u6e90
6\u3001\u6839\u636e\u5fd9\u788c\u65f6\u95f4\u5360\u603b\u65f6\u95f4\u7684\u6bd4\u4f8b\u6765\u8ba1\u7b97cpu\u4f7f\u7528\u7387
\u793a\u4f8b\uff1a
private double getCpuRatioForWindows() { try { String procCmd = System.getenv("windir") + "//system32//wbem//wmic.exe process get Caption,CommandLine," + "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount"; // \u53d6\u8fdb\u7a0b\u4fe1\u606f 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; } }

Eclipse\u9ed8\u8ba4\u628a\u8fd9\u4e9b\u53d7\u8bbf\u95ee\u9650\u5236\u7684API\u8bbe\u6210\u4e86ERROR\u3002\u53ea\u8981\u628aWindows-Preferences-Java-Complicer-Errors/Warnings\u91cc\u9762\u7684Deprecated and restricted API\u4e2d\u7684Forbidden references(access rules)\u9009\u4e3aWarning\u5c31\u53ef\u4ee5\u7f16\u8bd1\u901a\u8fc7\u3002

java获取所有系统信息(CPU、内存、进程等)的代码:
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;

import mytools.com.sun.management.OperatingSystemMXBean;
import mytools.java.io.File;
import mytools.java.lang.management.ManagementFactory;
/**
* 获取windows系统信息(CPU,内存,文件系统)
* @author libing
*
*/

public class WindowsInfoUtil {
private static final int CPUTIME = 500;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;

public static void main(String[] args) {
System.out.println(getCpuRatioForWindows());
System.out.println(getMemery());
System.out.println(getDisk());
}

//获取内存使用率
public static String getMemery(){
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
// 总的物理内存+虚拟内存
long totalvirtualMemory = osmxb.getTotalSwapSpaceSize();
// 剩余的物理内存
long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
Double compare=(Double)(1-freePhysicalMemorySize*1.0/totalvirtualMemory)*100;
String str="内存已使用:"+compare.intValue()+"%";
return str;
}

//获取文件系统使用率
public static List<String> getDisk() {
// 操作系统
List<String> list=new ArrayList<String>();
for (char c = 'A'; c <= 'Z'; c++) {
String dirName = c + ":/";
File win = new File(dirName);
if(win.exists()){
long total=(long)win.getTotalSpace();
long free=(long)win.getFreeSpace();
Double compare=(Double)(1-free*1.0/total)*100;
String str=c+":盘 已使用 "+compare.intValue()+"%";
list.add(str);
}
}
return list;
}

//获得cpu使用率
public static String 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 "CPU使用率:"+Double.valueOf(PERCENT * (busytime)*1.0 / (busytime + idletime)).intValue()+"%";
} else {
return "CPU使用率:"+0+"%";
}
} catch (Exception ex) {
ex.printStackTrace();
return "CPU使用率:"+0+"%";
}
}

//读取cpu相关信息
private static long[] readCpu(final Process proc) {
long[] retn = new long[2];
try {
proc.getOutputStream().close();
InputStreamReader ir = new InputStreamReader(proc.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line = input.readLine();
if (line == null || line.length() < FAULTLENGTH) {
return null;
}
int capidx = line.indexOf("Caption");
int cmdidx = line.indexOf("CommandLine");
int rocidx = line.indexOf("ReadOperationCount");
int umtidx = line.indexOf("UserModeTime");
int kmtidx = line.indexOf("KernelModeTime");
int wocidx = line.indexOf("WriteOperationCount");
long idletime = 0;
long kneltime = 0;
long usertime = 0;
while ((line = input.readLine()) != null) {
if (line.length() < wocidx) {
continue;
}
// 字段出现顺序:Caption,CommandLine,KernelModeTime,ReadOperationCount,
// ThreadCount,UserModeTime,WriteOperation
String caption =substring(line, capidx, cmdidx - 1).trim();
String cmd = substring(line, cmdidx, kmtidx - 1).trim();
if (cmd.indexOf("wmic.exe") >= 0) {
continue;
}
String s1 = substring(line, kmtidx, rocidx - 1).trim();
String s2 = substring(line, umtidx, wocidx - 1).trim();
if (caption.equals("System Idle Process") || caption.equals("System")) {
if (s1.length() > 0)
idletime += Long.valueOf(s1).longValue();
if (s2.length() > 0)
idletime += Long.valueOf(s2).longValue();
continue;
}
if (s1.length() > 0)
kneltime += Long.valueOf(s1).longValue();
if (s2.length() > 0)
usertime += Long.valueOf(s2).longValue();
}
retn[0] = idletime;
retn[1] = kneltime + usertime;
return retn;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
proc.getInputStream().close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}

/**
* 由于String.subString对汉字处理存在问题(把一个汉字视为一个字节),因此在 包含汉字的字符串时存在隐患,现调整如下:
* @param src 要截取的字符串
* @param start_idx 开始坐标(包括该坐标)
* @param end_idx 截止坐标(包括该坐标)
* @return
*/
private static String substring(String src, int start_idx, int end_idx) {
byte[] b = src.getBytes();
String tgt = "";
for (int i = start_idx; i <= end_idx; i++) {
tgt += (char) b[i];
}
return tgt;
}
}

安装了java虚拟机以后的目录下有一个Demo文件夹,里面有很多范例,其中就有读取CPU信息,楼主可以找一找。
满意请采纳,谢谢你的支持!!!(๑•̀ㅂ•́)و✧

亲.java的目录下有一个Demo文件夹,里面有很多范例,其中就有读取CPU信息,望采纳点赞谢谢

安装了java虚拟机以后的目录下有一个Demo文件夹,里面有很多范例,其中就有读取CPU信息,楼主可以找一找。

Motherboard serial number

import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MiscUtils {
private MiscUtils() { }

public static String getMotherboardSN() {
String result = "";
try {
File file = File.createTempFile("realhowto",".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);

String vbs =
"Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
+ "Set colItems = objWMIService.ExecQuery _ \n"
+ " (\"Select * from Win32_BaseBoard\") \n"
+ "For Each objItem in colItems \n"
+ " Wscript.Echo objItem.SerialNumber \n"
+ " exit for ' do the first cpu only! \n"
+ "Next \n";

fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
}
catch(Exception e){
e.printStackTrace();
}
return result.trim();
}

public static void main(String[] args){
String cpuId = MiscUtils.getMotherboardSN();
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
null, cpuId, "Motherboard serial number",
javax.swing.JOptionPane.DEFAULT_OPTION);
}
}

你试一试看看这段代码能否满足你的要求

  • java 鑳戒笉鑳鑾峰彇CPU鐨ID鍙,纭洏鐨勫簭鍒楀彿
    绛旓細public class CpuUtil { public static void main(String[] args) throws IOException { long start = System.currentTimeMillis(); Process process = Runtime.getRuntime().exec( new String[] { "wmic", "cpu", "get", "ProcessorId" }); process.getOutputStream().close(); Sc...
  • java 濡備綍鑾峰緱涓涓繘绋嬬殑鍐呭瓨浣跨敤鎯呭喌,cpu杩愯鐨勬椂闂
    绛旓細鈶 long someThreadCpuTime = threadMXBean.getThreadCpuTime(someThreadId); //鑾峰彇ID涓簊omeThreadId鍗709817鐨勭嚎绋嬬殑cpu鏃堕棿 鍩轰簬涓婇潰鐨勬牳蹇僡pi锛屼綘鍙互鎶婄敱java鍚姩鐨勫閮ㄨ繘绋嬫斁鍒颁竴涓崟鐙殑绾跨▼涓墽琛岋紝鍐嶇敤浠g爜鈶$殑鏂瑰紡鏉ヨ幏鍙栬杩涚▼鐨刢pu浣跨敤鏃堕棿锛屼篃鍙互灏嗗閮ㄨ繘绋嬫斁鍏ュ埌褰撳墠绾跨▼涓墽琛岋紝鐢ㄢ憼 鐨勬柟寮...
  • 鐢Java缂栫▼鏉ュ疄鐜CPU鍜孧emory鐩戞帶鍣ㄥ苟灏鏁版嵁鎸佷箙鍖,淇濆瓨鍦ㄦ枃浠朵腑銆傛...
    绛旓細棣栧厛寤轰竴涓枃鏈枃浠讹紝姣斿鍦―鐩樻牴鐩綍涓嬶紝鍚嶅瓧涓猴細int.txt锛岀劧鍚庡啓涓涓Java绫籆alcNumber.java銆***int.txt閲岄潰鐨勬枃鏈涓嬶細1 2 3 4 5 6 7 8 9 1011 12 13 14 15 16 17 18 19 2021 22 23 24 25 26 27 28 29 3031 32 33 34 35 36 37 38 39 4041 42 43 44 45 46 47 48 49...
  • java鑾峰彇cpu娓╁害,璺眰婧愪唬鐮併傘傘
    绛旓細Scanner(p.getInputStream());sc.next();float t=(sc.nextInt()-2732f)/10f;sc.close();System.out.println("褰撳墠CPU娓╁害:"+t);p.waitFor();} catch (Exception e) { e.printStackTrace();} } } == 褰撳墠CPU娓╁害:41.0 linux涓嬪悓鏍蜂緷璧栤滀富鏉块┍鍔ㄢ, 鏁版嵁鎻愪緵缁/dev/sensor ...
  • 濡備綍鑾峰彇鐢佃剳鐨CPU淇℃伅
    绛旓細鐨勫唴瀹圭殑浜鸿繕鐪嬶細1. 鑾峰彇纭欢淇℃伅鏂规硶 2. js鎬庝箞鑾峰彇鐢佃剳纭欢淇℃伅 3. 鑾峰彇璁$畻鏈虹‖浠朵俊鎭柟娉 4. 鑾峰彇鐢佃剳纭欢淇℃伅鏂规硶 5. 濡備綍蹇熸煡鐪嬬數鑴戠‖浠朵俊鎭 6. 濡備綍鏌ョ湅鐢佃剳鐨勭‖浠朵俊鎭 7. 鎬庝箞鏌ョ湅璁$畻鏈虹‖浠朵俊鎭 8. 鏌ョ湅鐢佃剳纭欢淇℃伅鏂规硶 9. 璁$畻鏈虹‖浠朵俊鎭庝箞鏌ョ湅 10. java鎬庝箞鑾峰彇鐢佃剳纭欢淇℃伅 ...
  • java snmp4j鏈夌煡閬撶殑鍚?鎴戞兂鑾峰彇鏈満鐨cpu銆佸唴瀛樼瓑淇℃伅鎬绘槸鑾峰彇涓嶅埌...
    绛旓細鎴戠幇鍦ㄤ篃鍦ㄥ仛java snmp4j鍘昏澶囧硷紝鍙傝冧簡涓涓嬬櫨搴︾粰鍑虹殑渚嬪瓙锛屾兂瑕佷粠纭欢璁惧閲鑾峰彇淇℃伅濂藉儚寰楅渶瑕佹湁瀵瑰簲鐨勮妭鐐瑰硷紝鍏蜂綋鐨勬垜涔熶笉鏄緢娓呮銆傚垰鎺ヨЕSNMP鍗忚锛屽笇鏈涜兘涓璧蜂氦娴佷竴涓嬨
  • java鑳鑾峰彇鐢佃剳鐨勫搧鐗屽悧
    绛旓細/** 鎬荤殑鐗╃悊鍐呭瓨. */ private float totalMemorySize; /** 宸蹭娇鐢ㄧ殑鐗╃悊鍐呭瓨. */ private float usedMemory; /** cpu浣跨敤鐜. */ private double cpuRatio; /** 涓绘満IP鍦板潃 */ private String mIpAddress; /** 鏁版嵁瀛樺偍鏃堕棿 */ private String dDateTime; /**鍐呭瓨浣跨敤鐜...
  • Java濡備綍鑾峰彇CPU鏍囪瘑绗
    绛旓細鍙皾璇曢氳繃System.getProperties() 杩欑被API鑾峰彇锛歋ystem.getProperty("os.arch") -- cpu鏋舵瀯 锛屽x86 os.name --濡俉indows XP os.version -- 鎿嶄綔绯荤粺鐗堟湰鍙 濡傛灉杩樻棤娉曟弧瓒筹紝鍙互鏍规嵁绯荤粺鐨勪笉鍚岋紝鍒╃敤Runtime鍏ュ彛鍘绘墽琛屽懡浠ゆ潵鑾峰彇锛屾瘮濡傦細linux 涓 鍙互鎵ц濡傦細Runtime.getRuntime().exec("cat...
  • 璇烽棶Java濡備綍鍔ㄦ佹樉绀哄綋鍓嶅唴瀛樺崰鐢ㄧ巼楗煎浘
    绛旓細Java浠g爜鍒╃敤java绋嬪簭瀹炵幇鑾峰彇璁$畻鏈cpu鍒╃敤鐜囧拰鍐呭瓨浣跨敤淇℃伅銆 鍒涘缓涓涓狟ean鐢ㄦ潵瀛樿串瑕佸緱鍒鐨勪俊 public class MonitorInfoBean { /** 鍙娇鐢ㄥ唴瀛. */ private long totalMemory; /** 鍓╀綑鍐呭瓨. */ private long freeMemory; /** 鏈澶у彲浣跨敤鍐呭瓨. */ private long maxMemory; /** 鎿嶄綔绯荤粺. */ private ...
  • Java濡備綍瀹氫綅鍗犵敤CPU姣旇緝楂樼殑闂
    绛旓細2銆乯stack杈撳嚭涓哄綋鍓嶇灛闂寸殑鍫嗘爤淇℃伅锛屽鏋滈亣鍒伴棿鏂у嚭鐜CPU楂樼殑闂鏃讹紝闇瑕佸杈撳嚭鍑犳 浠庝笂闈㈡柟寮忓畾浣嶅埌浠g爜Test.main(Test.java:4)澶勫鑷翠簡CPU鍋忛珮鐨勯棶棰橈紝閭f垜浠煡鐪嬩笅浠g爜鍏蜂綋濡備綍瀹炵幇鐨勶紵浠g爜瀹炵幇锛歱ublic class Test {public static void main(String[] args) {while(true) {}}} 浠庝唬鐮佸眰闈㈢湅...
  • 扩展阅读:java 垃圾回收 cpu占用 ... java进程占用cpu过高 ... 为啥都不建议学软件测试 ... java手动输入数据 ... java自学要学多久 ... java吃cpu还是内存 ... 学java为什么27岁就迟了 ... php和java哪个吃香 ... 为什么都不建议java转测试 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网