养生 装修 购物 美食 感冒 便秘 营销 加盟 小吃 火锅 管理 创业 搭配 减肥 培训 旅游

java获取windowscpu使用率方法

时间:2024-09-21 12:44:19

java不是系统级的语言,所以获取系统信息需要用一些系统命令,这里就分享怎么用java获取cpu信息

工具/原料

java

方法/步骤

1、第一步:判断系统,linux,windons,mac等等系统使用命令和方法都不一样。代码如下://操作系统StringosName=System.getProperty("os.name");

java获取windowscpu使用率方法

2、第二步:获取系统内存信息,代码如下://可使用内存longtotalMemory=Runtime.getRuntime().totalMemory()/kb;//剩余内存longfreeMemory=Runtime.getRuntime().freeMemory()/kb;//最大可使用内存longmaxMemory=Runtime.getRuntime().maxMemory()/kb;

java获取windowscpu使用率方法

3、第三步:获取cpu信息,命令和代码如下:StringprocCmd=System.getenv("windir")+"//system32//wbem//wmic.exeprocessgetCaption,CommandLine,"+"KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";//取进程信息long[]c0=readCpu(Runtime.getRuntime().exec(procCmd));

java获取windowscpu使用率方法

4、第四步:readCpu方法代码如下:privatelong[]readCpu(fina盟敢势袂lProcessproc)撑俯擂摔{long[]retn=newlong[2];try{proc.getOutputStream().close();InputStreamReaderir=newInputStreamReader(proc.getInputStream());LineNumberReaderinput=newLineNumberReader(ir);Stringline=input.readLine();if(line==null||line.length()<FAULTLENGTH){returnnull;}intcapidx=line.indexOf("Caption");intcmdidx=line.indexOf("CommandLine");introcidx=line.indexOf("ReadOperationCount");intumtidx=line.indexOf("UserModeTime");intkmtidx=line.indexOf("KernelModeTime");intwocidx=line.indexOf("WriteOperationCount");longidletime=0;longkneltime=0;longusertime=0;while((line=input.readLine())!=null){if(line.length()<wocidx){continue;}Stringcaption=Bytes.substring(line,capidx,cmdidx-1).trim();Stringcmd=Bytes.substring(line,cmdidx,kmtidx-1).trim();if(cmd.indexOf("wmic.exe")>=0){continue;}if(caption.equals("SystemIdleProcess")||caption.equals("System")){idletime+=Long.valueOf(Bytes.substring(line,kmtidx,rocidx-1).trim()).longValue();idletime+=Long.valueOf(Bytes.substring(line,umtidx,wocidx-1).trim()).longValue();continue;}kneltime+=Long.valueOf(Bytes.substring(line,kmtidx,rocidx-1).trim()).longValue();usertime+=Long.valueOf(Bytes.substring(line,umtidx,wocidx-1).trim()).longValue();}retn[0]=idletime;retn[1]=kneltime+usertime;returnretn;}catch(Exceptionex){ex.printStackTrace();}finally{try{proc.getInputStream().close();}catch(Exceptione){e.printStackTrace();}}returnnull;}

java获取windowscpu使用率方法

5、第五步:构造对象,对返回的结果进行封装,代码如下://构造返回对象MonitorInfoBeaninfoBean=newMonitorInfoBean();infoBean.setFreeMemory(freeMemory);infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize);infoBean.setMaxMemory(maxMemory);infoBean.setOsName(osName);infoBean.setTotalMemory(totalMemory);infoBean.setTotalMemorySize(totalMemorySize);infoBean.setTotalThread(totalThread);infoBean.setUsedMemory(usedMemory);infoBean.setCpuRatio(cpuRatio);

java获取windowscpu使用率方法

6、第六步:MonitorInfoBean实体类代码如下:@Datapublicclass咯悝滩镞MonitorInfoBean{/***//**可使用内存.惺绅寨瞀*/privatelongtotalMemory;/***//**剩余内存.*/privatelongfreeMemory;/***//**最大可使用内存.*/privatelongmaxMemory;/***//**操作系统.*/privateStringosName;/***//**总的物理内存.*/privatelongtotalMemorySize;/***//**剩余的物理内存.*/privatelongfreePhysicalMemorySize;/***//**已使用的物理内存.*/privatelongusedMemory;/***//**线程总数.*/privateinttotalThread;/***//**cpu使用率.*/privatedoublecpuRatio;}

java获取windowscpu使用率方法

7、第七步:运行结果展示,如图:

java获取windowscpu使用率方法

8、第八步:此放不了网盘地址,要的留言

© 一点知识