用java获取CPU占用率
废话不说,代码如下:
Bytes.java
引用:
Class Bytes{
public static String substring(Stringsrc,intstart_idx,intend_idx){ byte[]b=src.getBytes(); Stringtgt=\
for(inti=start_idx;i<=end_idx;i++){ tgt+=(char)b; } returntgt; } } MonitorInfoBean.java 引用: Public class MonitorInfoBean{ /**线程总数.*/ private int totalThread; /**cpu使用率.*/ private double cpuRatio; public int getTotalThread(){ return totalThread; } public void setTotalThread(int totalThread){ this.totalThread=totalThread; } publicdoublegetCpuRatio(){ returncpuRatio; } publicvoidsetCpuRatio(doublecpuRatio){ this.cpuRatio=cpuRatio; } } CPU.java 引用: importjava.io.BufferedReader; importjava.io.IOException; importjava.io.InputStream; importjava.io.InputStreamReader; importjava.io.LineNumberReader; importjava.util.StringTokenizer; Public class CPU{ private static finalint CPUTIME=30; private static finalint PERCENT=100; private static finalint FAULTLENGTH=10; private static String linuxVersion=null; public double getCpuRatio(){ //操作系统 StringosName=System.getProperty(\double cpuRatio=0; if(osName.toLowerCase().startsWith(\cpuRatio=this.getCpuRatioForWindows(); }else{ cpuRatio=this.getCpuRateForLinux(); } return cpuRatio; } /** *获得当前的监控对象. * *@return返回构造好的监控对象 */ publicMonitorInfoBeangetMonitorInfoBean()throwsException{ //操作系统 StringosName=System.getProperty(\ //获得线程总数 ThreadGroupparentThread;for(parentThread=Thread.currentThread().getThreadGroup();parentThread .getParent()!=null;parentThread=parentThread.getParent()) ; inttotalThread=parentThread.activeCount(); doublecpuRatio=0; if(osName.toLowerCase().startsWith(\cpuRatio=this.getCpuRatioForWindows(); }else{ cpuRatio=this.getCpuRateForLinux(); } //构造返回对象 MonitorInfoBeaninfoBean=newMonitorInfoBean(); infoBean.setTotalThread(totalThread); infoBean.setCpuRatio(cpuRatio); returninfoBean; } private static doubleg etCpuRateForLinux(){ InputStreamis=null; InputStreamReaderisr=null; BufferedReaderbrStat=null; StringTokenizertokenStat=null; try{ System.out.println(\+linuxVersion); Processprocess=Runtime.getRuntime().exec(\is=process.getInputStream(); isr=newInputStreamReader(is); brStat=newBufferedReader(isr); if(linuxVersion.equals(\brStat.readLine(); brStat.readLine(); brStat.readLine(); brStat.readLine(); tokenStat=newStringTokenizer(brStat.readLine()); tokenStat.nextToken(); tokenStat.nextToken(); Stringuser=tokenStat.nextToken(); tokenStat.nextToken(); Stringsystem=tokenStat.nextToken(); tokenStat.nextToken(); Stringnice=tokenStat.nextToken(); System.out.println(user+\ user=user.substring(0,user.indexOf(\system=system.substring(0,system.indexOf(\nice=nice.substring(0,nice.indexOf(\ floatuserUsage=newFloat(user).floatValue(); floatsystemUsage=newFloat(system).floatValue(); floatniceUsage=newFloat(nice).floatValue(); return(userUsage+systemUsage+niceUsage)/100; }else{ brStat.readLine(); brStat.readLine(); tokenStat=newStringTokenizer(brStat.readLine()); tokenStat.nextToken(); tokenStat.nextToken(); tokenStat.nextToken(); tokenStat.nextToken(); tokenStat.nextToken(); tokenStat.nextToken(); tokenStat.nextToken(); StringcpuUsage=tokenStat.nextToken(); System.out.println(\Floatusage=newFloat(cpuUsage.substring(0,cpuUsage .indexOf(\ return(1-usage.floatValue()/100); } }catch(IOExceptionioe){ System.out.println(ioe.getMessage()); freeResource(is,isr,brStat); return1; }finally{ freeResource(is,isr,brStat); } } privatestaticvoidfreeResource(InputStreamis,InputStreamReaderisr, BufferedReaderbr){ try{ if(is!=null) is.close(); if(isr!=null) isr.close(); if(br!=null) br.close(); }catch(IOExceptionioe){ System.out.println(ioe.getMessage()); } } /** *获得CPU使用率. * *@return返回cpu使用率 */ private double getCpuRatioForWindows(){ try{ StringprocCmd=System.getenv(\+\+\rationCount\//取进程信息
long[]c0=readCpu(Runtime.getRuntime().exec(procCmd)); Thread.sleep(CPUTIME); long[]c1=readCpu(Runtime.getRuntime().exec(procCmd)); if(c0!=null&&c1!=null){ longidletime=c1[0]-c0[0]; longbusytime=c1[1]-c0[1]; returnDouble.valueOf( PERCENT*(busytime)/(busytime+idletime)) .doubleValue(); }else{ return0.0; } }catch(Exceptionex){ ex.printStackTrace(); return0.0; } } /** * *读取CPU信息. * *@paramproc */ privatelong[]readCpu(finalProcessproc){ long[]retn=newlong[2]; try{ proc.getOutputStream().close(); InputStreamReaderir=newInputStreamReader(proc.getInputStream()); LineNumberReaderinput=newLineNumberReader(ir); Stringline=input.readLine(); if(line==null||line.length() 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库用java获取CPU占用率在线全文阅读。
相关推荐: