Chinaunix首页 | 论坛 | 博客
  • 博客访问: 983093
  • 博文数量: 75
  • 博客积分: 9980
  • 博客等级: 中将
  • 技术积分: 4525
  • 用 户 组: 普通用户
  • 注册时间: 2004-11-10 14:01
文章分类

全部博文(75)

文章存档

2010年(20)

2009年(31)

2008年(24)

我的朋友

分类: Java

2008-03-18 18:02:52

偶尔从一本杂志上看到的一个问题,挺有意思。
这个问题貌似挺玄,实际上很简单。在某一时刻,CPU只有两种状态,busy or idle。所谓的保持CPU使用率在50%左右只是说,如果恰恰在一个CPU使用率的统计周期内,CPU一半时间busy,一半idle,则在windows任务管理器中显示的CPU使用记录就是50%了。下面这段代码就能实现该功能:
 public static void firstMethod() throws InterruptedException {
  for (;;) {
   for (int i = 0; i <= 9600000; i++)
    ;
   Thread.sleep(10);
  }
 }
当然,根据CPU的运算能力不同,9600000这个值需要调整。这也正是上面代码的缺点,下面这个方法能保证CPU一半时间busy一半时间idle:
 public static void secondMethod() throws InterruptedException {
  int busyTime = 10;
  int idleTime = 2 * busyTime;
  long startTime = 0;
  while (true) {
   startTime = System.currentTimeMillis();
   while (System.currentTimeMillis() - startTime <= busyTime)
    ;
   Thread.sleep(idleTime);
  }
 }
但是第二个方法也有问题,就是如果CPU已经有一定的使用率,比如正在运行其它程序,就会不准确。那么要动态的,准确的决定CPU的idle和busy时间,就要先取得当前CPU的使用率,这个又因不同的操作系统而异,比如Linux下,也许可以这样得到:
  Runtime rt = Runtime.getRuntime();
  Process p = rt.exec("top -b -n 1");
  然后再操作p的inputStream就可以得到想要的值。如果是在windows上,恐怕就要借助于JNI(Java Native Interface)了。
下面这个方法能让CPU使用率的曲线呈现正弦曲线,原理就是让CPU busy和idle的时间按照正弦曲线的坐标值此消彼长:
 public static void thirdMethod() throws InterruptedException {
  final double SPLIT = 0.01;
  final int COUNT = 200;
  final double PI = 3.14159265;
  final int INTERVAL = 300;
  int[] busySpan = new int[COUNT];
  int[] idleSpan = new int[COUNT];
  int half = INTERVAL / 2;
  double radian = 0.0;
  for (int i = 0; i < COUNT; i++) {
   busySpan[i] = (half + (int)(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])
    ;
   Thread.sleep(idleSpan[j]);
   j++;
  }
 }
阅读(9017) | 评论(1) | 转发(0) |
0

上一篇:The Longest Day To My Wife

下一篇:当当当当

给主人留下些什么吧!~~

chinaunix网友2009-11-05 19:43:14

看不懂