testcpu.java

来自「这是一个在linux环境下」· Java 代码 · 共 69 行

JAVA
69
字号
package fr.umlv.projet.procfs;/** *  * @author MA Xiao Jun & HUANG Wei * */public class TestCpu {		private static CpuTime oldtime = new CpuTime();	private static CpuTime newtime = new CpuTime();//	private static float cpuUsed = 0;	public static CpuTime getoldtime(){		return oldtime;	}		public static CpuTime getnewtime(){		return newtime;	}	public static float calcul(CpuTime oldtime , CpuTime newtime){		float use = newtime.getUserTime() - oldtime.getUserTime(); 		float system = newtime.getSystemTime() - oldtime.getSystemTime();		return ((use + system)*100.0f)/(newtime.getTotalTime() - oldtime.getTotalTime());	}			public static String changeFloatSize(float value,int size){		String s = new String(value+"");		int index = s.indexOf('.');		float num = 0.5f;		if(s.length()>index+size+1){			if(s.charAt(index+size+1)>'4'){				for(int i=0;i<size;i++)					num *= 0.1f;				value += num;			}		s = new String(value+"");		String s1 = s.substring(0,index+size+1);		return s1;}		else return new String(value+"");	}	//	public static String getCpuUsed(){//		//	}		public static void main(String[] args) {		oldtime.loadCpuTime();		try {			Thread.sleep(3000);		} catch (InterruptedException e) {			e.printStackTrace();		}		while(true){			newtime.loadCpuTime();			System.out.println(calcul(oldtime,newtime));			oldtime.setTime(newtime);			try {				Thread.sleep(3000);			} catch (InterruptedException e) {				e.printStackTrace();			}		}//		float num = .126624f;//		System.out.println(changeFloatSize(num,3));	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?