zoukankan      html  css  js  c++  java
  • 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的值。有思路的青年请给个留言。 

  • 相关阅读:
    Zookeeper中Watcher监听实现增删改
    Zookeeper
    pyspider爬豆瓣电影实例
    纵表与横表转换(实用)
    完善爬取糗百的段子
    为自己的爬虫更换代理和HTML头部
    我的第一个爬虫(爬取糗百的段子)
    django创建blog
    python-study-23
    python-study-22
  • 原文地址:https://www.cnblogs.com/fanstatic/p/2753003.html
Copyright © 2011-2022 走看看