Java 查看Windows下CPU利用率

1 篇文章 0 订阅
package com.test;

import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class Test {
	private static final int CPUTIME = 500;
	private static final int PERCENT = 100;
	private static final int FAULTLENGTH = 10;
	
	//读取cpu相关信息 
	public 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 截止坐标(包括该坐标)
	 * */
	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; 
	} 
	
	public static void main(String[] args) throws Exception {
		//CPU
		String cmd = System.getenv("windir") + "\\system32\\wbem\\wmic.exe process get Caption,CommandLine,KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
		//获取进程信息
		long[] c0 = readCPU(Runtime.getRuntime().exec(cmd));
		Thread.sleep(CPUTIME);
		long[] c1 = readCPU(Runtime.getRuntime().exec(cmd));
		if(c0 != null && c1 != null){
			long idleTime = c1[0] - c0[0];
			long useTime = c1[1] - c0[1];
			System.out.println("CPU使用率:" + Double.valueOf(PERCENT * useTime / (idleTime + useTime)).intValue() + "%");
		}
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值