当前位置:编程学习 > JAVA >>

编程之美_003让CPU占用率划出一条直线

[java] 
// CPU使用率其实就是你运行的程序占用的CPU资源,表示你的机器在某个时间点的运行程序的情况。  
public class CPUTest  
{  
    public static void main(String[] args)  
    {  
        long startTime = 0;// 开始时间  
        int busyTime = 10;// 繁忙时间  
        int idleTime = 10;// 空闲时间  
        while (true)  
        {  
            startTime = System.currentTimeMillis();  
            // CPU繁忙  
            while (System.currentTimeMillis() - startTime <= busyTime)  
                ;  
            // CPU空闲  
            try  
            {  
                Thread.sleep(idleTime);  
            }  
            catch (InterruptedException e)  
            {  
                e.printStackTrace();  
            }  
        }  
    }  
}  
 
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,