编程之美一 : 让CPU占有率曲线听你指挥
来源:互联网 发布:散热器优化仿真 编辑:程序博客网 时间:2024/05/25 05:37
写一个程序,让用户来决定Windows任务管理器(Task Manager)的CPU占有率。程序越精简越好,可以实现以下三种情况:
/**** * * 1. JAVA控制CPU的占有率 - 固定在50%,为一条直线 * ****/public class CPUTest1 {public static void main(String[] args) throws Exception{for (;;) {for (int i = 0; i < 96000000; i++) {;}Thread.sleep(10);}}}
/**** * * 2. JAVA控制CPU的占有率 - 控制在50% * ****/public class CPUTest2 {static int busyTime = 10;static int idelTime = busyTime; // 50%的占有率public static void main(String[] args) throws Exception {long startTime = 0;while (true) {startTime = System.currentTimeMillis();while (System.currentTimeMillis() - startTime < busyTime) {;}Thread.sleep(idelTime);}}}
/**** * 3. JAVA控制CPU的使用率 - 完美曲线 * * 把一条正弦曲线0~2π之间的弧度等分成200份进行抽样,计算每个抽样点的数据 * 然后每隔300ms的时间取下一个抽样点,并让cpu工作对应振幅的时间 * ****/public class CPUTest3 {public static final int SAMPLING_COUNT = 200; // 抽样点数量 2/RANDIAN_INCREMENTpublic static final double PI = Math.PI; // pi值public static final double RANDIAN_INCREMENT = 0.01; // 抽样弧度的增量, 2/SAMPLING_COUNTpublic static final int TOTAL_AMPLITUDE = 300; // 振幅, 每个抽样点对应的时间片public static void main(String[] args) throws Exception {// 角度的分割 long[] busySpan = new long[SAMPLING_COUNT];long[] idleSpan = new long[SAMPLING_COUNT];int amplitude = TOTAL_AMPLITUDE / 2;double radian = 0.0;for (int i = 0; i < SAMPLING_COUNT; i++) {busySpan[i] = (long) (amplitude + (Math.sin(PI * radian) * amplitude));radian += RANDIAN_INCREMENT;}long startTime = 0;for (int j = 0;; j = (j + 1) % SAMPLING_COUNT) {startTime = System.currentTimeMillis();while (System.currentTimeMillis() - startTime < busySpan[j]) {;}Thread.sleep(idleSpan[j]);}}}
- 编程之美一 : 让CPU占有率曲线听你指挥
- 编程之美一 : 让CPU占有率曲线听你指挥
- 《编程之美》读书随笔之一:让CPU占有率曲线听你指挥
- 编程之美之一(让cpu占有率曲线听你指挥)
- 让cpu占有率曲线听你指挥
- 让CPU占有率曲线听你指挥
- 编程之美 - 让CPU占用率曲线听你指挥
- 编程之美:让CPU占用率曲线听你指挥
- 编程之美:让CPU占用率曲线听你指挥
- 编程之美:让CPU占用率曲线听你指挥
- 编程之美:让CPU占用率曲线听你指挥
- 编程之美:让CPU占用率曲线听你指挥
- 编程之美:让CPU占用率曲线听你指挥
- 编程之美:让CPU占用率曲线听你指挥
- 编程之美:让CPU占用率曲线听你指挥
- 编程之美:让CPU占用率曲线听你指挥
- 编程之美1.1让CPU占用率曲线听你指挥之解法一细节
- 编程之美学习笔记(一)-让CPU占用率曲线听你指挥
- C# windows 服务(转)
- Cocos2dx Cygwin编译-格式问题“CocosDenshion/android/SimpleAudioEngine” make error解决方法
- FFT 变化
- GCC 参数详解
- 异或运算交换两个元素位置,不需要额外空间
- 编程之美一 : 让CPU占有率曲线听你指挥
- 修改终端提示信息
- HDU 4302 Holedox Eating (两个优先队列)
- 内存管理之伙伴算法
- ASCII码表 0-127
- 动态规划--电路布线
- 去哪儿网2014笔试算法题汇总
- JAVA和JAVAC 命令行
- 在ACM中使用Java的若干方便之处