• 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("一个周期"); } }
  • 相关阅读:
    无线渗透开启WPS功能的路由器
    写代码怎能不会这些Linux命令?
    分布式服务框架 Zookeeper -- 管理分布式环境中的数据
    每天进步一点点——五分钟理解一致性哈希算法(consistent hashing)
    Innodb 中的事务隔离级别和锁的关系
    线上操作与线上问题排查实战
    MySQL 四种事务隔离级的说明
    一次由于 MTU 设置不当导致的网络访问超时
    SYN 和 RTO
    The story of one latency spike
  • 原文地址:https://www.cnblogs.com/xumz/p/7460200.html
Copyright © 2020-2023  润新知