• cpu占用率曲线笔记


    我们知道要查看电脑的cpu占用率的话可以从任务管理器得知,进一步可以从->性能栏 看到CPU占用率的曲线的变化。今天看了编程之美的第一章第一节,看到对电脑cpu占用率的控制,控制CPU占用率曲线变化,达到特定的效果。

    cpu占用率曲线的刷新频率为1秒。要使在某一时刻CPU占用率达到目的中的百分比,可通过分配1秒内程序运行和休眠的时间的所占比,来达到。如在某1秒内,程序都处于休眠状态,那么,CPU占用率将达到0(理想化)。反正,cpu占用率将达到100%。

    第一种方法:基于计算程序所运行的电脑CPU主频,而得到的循环次数参数(根据电脑配置而定),该参数是10毫秒内,CPU运行循环40500000次。

    public static void main(String[] args) throws InterruptedException {
            for (;;){
                 for (int i=0;i<40500000;i++);
                 Thread.sleep(10);
             }
        }

    二:根据所占时间比重

    public static final int BUSYTIME=10;
    public static final int IDLETIME=BUSYTIME;
    public
    static void main(String[] args) throws InterruptedException { while(true){ long startTime=System.currentTimeMillis();//获取开始时间 while(System.currentTimeMillis()-startTime<=BUSYTIME){ }; Thread.sleep(IDLETIME); } }

    三:画个正弦

    public static final double PI=3.1415926535;
    public static final int X_COUNT=200;//200百个抽样点,画个正弦函数
    public static final int TIME=300;//每个点的时间片,单位毫秒
     //正弦函数Y=TIME/2 + TIME/2*Math.sin(i*PI/100)
     //思路:先计算每个时间片内,busy和idle分别所运行的时间,之后进行循环运行
    public
    static void main(String[] args) throws InterruptedException { int busy[]=new int[200]; for (int i = 0; i < X_COUNT; i++) { busy[i]=(int) (TIME/2 + TIME/2*Math.sin(i*PI/100)); System.out.println(busy[i]); } while(true){ for (int i = 0; i < busy.length; i++) { long startTime=System.currentTimeMillis();//获取开始时间 while(System.currentTimeMillis()-startTime<=busy[i]){ System.out.println(i); System.out.println(busy[i]); }; Thread.sleep(TIME-busy[i]); } System.out.println("一个周期"); } }
  • 相关阅读:
    PHP如何判断一个gif图片是否为动画?
    Linux常用系统管理命令(top、free、kill、df)
    Mysql字符串连接函数 CONCAT()与 CONCAT_WS()
    OSChina.net 的 Tomcat 配置 server.xml 参考
    修改Linux默认启动级别或模式
    更改CentOS 6.3 yum源为国内 阿里云源
    PHP session过期机制和配置
    PHP垃圾回收机制防止内存溢出
    memcache与memcached的区别
    【总结】虚拟机VirtualBox各种使用技巧
  • 原文地址:https://www.cnblogs.com/xumz/p/7460200.html
Copyright © 2020-2023  润新知