编程之美一 : 让CPU占有率曲线听你指挥

本文介绍三个使用Java编写的程序案例,分别实现固定50% CPU占用、动态控制50% CPU占用及模拟正弦波形变化的CPU占用。通过循环空操作与线程休眠控制CPU使用率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写一个程序,让用户来决定Windows任务管理器(Task Manager)的CPU占有率。程序越精简越好,可以实现以下三种情况:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /**** 
  2.  *  
  3.  * 1. JAVA控制CPU的占有率 - 固定在50%,为一条直线 
  4.  *  
  5.  ****/  
  6. public class CPUTest1 {  
  7.     public static void main(String[] args) throws Exception{  
  8.         for (;;) {  
  9.             for (int i = 0; i < 96000000; i++) {  
  10.                 ;  
  11.             }  
  12.             Thread.sleep(10);  
  13.         }  
  14.     }  
  15. }  

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /**** 
  2.  *  
  3.  * 2. JAVA控制CPU的占有率 - 控制在50% 
  4.  *  
  5.  ****/  
  6. public class CPUTest2 {  
  7.     static int busyTime = 10;  
  8.     static int idelTime = busyTime; // 50%的占有率  
  9.   
  10.     public static void main(String[] args) throws Exception {  
  11.         long startTime = 0;  
  12.         while (true) {  
  13.             startTime = System.currentTimeMillis();  
  14.             while (System.currentTimeMillis() - startTime < busyTime) {  
  15.                 ;  
  16.             }  
  17.             Thread.sleep(idelTime);  
  18.         }  
  19.     }  
  20. }  

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /**** 
  2.  * 3. JAVA控制CPU的使用率 - 完美曲线 
  3.  *  
  4.  * 把一条正弦曲线0~2π之间的弧度等分成200份进行抽样,计算每个抽样点的数据  
  5.  * 然后每隔300ms的时间取下一个抽样点,并让cpu工作对应振幅的时间 
  6.  *  
  7.  ****/  
  8. public class CPUTest3 {  
  9.     public static final int SAMPLING_COUNT = 200// 抽样点数量 2/RANDIAN_INCREMENT  
  10.     public static final double PI = Math.PI; // pi值  
  11.     public static final double RANDIAN_INCREMENT = 0.01// 抽样弧度的增量, 2/SAMPLING_COUNT  
  12.     public static final int TOTAL_AMPLITUDE = 300// 振幅, 每个抽样点对应的时间片  
  13.   
  14.     public static void main(String[] args) throws Exception {//  角度的分割     
  15.         long[] busySpan = new long[SAMPLING_COUNT];  
  16.         long[] idleSpan = new long[SAMPLING_COUNT];  
  17.         int amplitude = TOTAL_AMPLITUDE / 2;  
  18.         double radian = 0.0;  
  19.         for (int i = 0; i < SAMPLING_COUNT; i++) {  
  20.             busySpan[i] = (long) (amplitude + (Math.sin(PI * radian) * amplitude));  
  21.             radian += RANDIAN_INCREMENT;  
  22.         }  
  23.         long startTime = 0;  
  24.         for (int j = 0;; j = (j + 1) % SAMPLING_COUNT) {  
  25.             startTime = System.currentTimeMillis();  
  26.             while (System.currentTimeMillis() - startTime < busySpan[j]) {  
  27.                 ;  
  28.             }  
  29.             Thread.sleep(idleSpan[j]);  
  30.         }  
  31.     }  
  32. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值