您的位置:首页 > 编程语言 > Java开发

让CPU占用率曲线听你指挥

2013-10-05 11:27 423 查看
public static void main(String[] args) {

long startTime = 0;
while(true){
startTime = System.currentTimeMillis();

          while(System.currentTimeMillis()-startTime< 10);

          try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public static void testCPU() {
// 角度的分割
   final double SPLIT = 0.01;
   //
   // 2PI分割的次数,也就是2/0.01个,正好是一周
   final int COUNT = (int) (2 / SPLIT);
   final double PI = Math.PI;
   // 时间间隔
   final int INTERVAL = 200;
   long[] busySpan = new long[COUNT];
   long[] idleSpan = new long[COUNT];
   int half = INTERVAL / 2;
   double radian = 0.0;
   for (int i = 0; i < COUNT; i++) {
     busySpan[i] = (long) (half + (Math.sin(PI * radian) * half));
     idleSpan[i] = INTERVAL - busySpan[i];
     radian += SPLIT;
   }
   long startTime = 0;
   int j = 0;
   while (true) {
     j = j % COUNT;
     startTime = System.currentTimeMillis();
     while (System.currentTimeMillis() - startTime < busySpan[j])
       ;
     try {
Thread.sleep(idleSpan[j]);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
     j++;
   }
 
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java cpu