java让CPU一直保持直线形式

最近在看《编程之美》一书,感觉上面有很多值得学习的程序。现决定能尽量把所有的程序都一一以java的形式写出来。
可能网上已经出现了很多类似的代码。但我还是想坚持着写自己的程序。希望大家喜欢。 
/***
 * 编写一段java代码,使cpu始终保持一条直线
 * @author 朱贤军
 *
 */
public class P_006 {
public static void main(String[] args) throws Exception{
int busyTime = 10;
        int idleTime = 10;
        long startTime = 0;
        
        while (true) {  
            startTime = System.currentTimeMillis();  
        
            while ((System.currentTimeMillis() - startTime) <= busyTime) { 
            }
       
            try {  
                Thread.sleep(idleTime);  
            } catch (Exception e) {  
                System.out.println(e);  
            }  
        }  
}
}

经过测试,只能让CPU保持在50%,不知道能不能改进一下,可以通过参数改变CPU的值。有思路的青年请给个留言。 

原文地址:https://www.cnblogs.com/fanstatic/p/2753003.html