《编程之美》(Java实现) :让CPU占用率画直线和正弦曲线(Java实现)

  1. public class CPUTest {  
  2.       
  3.     //定义时间片大小(毫秒)  
  4.     public static final double TIME = 1000;  
  5.     //画直线方法  
  6.     private static void lineGraph(double rate) throws InterruptedException{  
  7.         while (true){  
  8.             doSomeSimpleWork(rate * TIME);  
  9.             Thread.sleep((long) (TIME - rate * TIME));  
  10.         }  
  11.     }  
  12.     //画正弦曲线方法  
  13.     private static void sinGraph() throws InterruptedException{  
  14.         double x = 0;  
  15.         double y = 0;         
  16.         while (true){  
  17.             y = (Math.sin(x) + 1) * TIME / 2;  
  18.             doSomeSimpleWork(y);  
  19.             x += 0.1;  
  20.             Thread.sleep((long) (TIME - y));  
  21.         }  
  22.     }  
  23.     //占用CPU方法  
  24.     private static void doSomeSimpleWork(double time) {  
  25.         long startTime = System.currentTimeMillis();  
  26.         while ((System.currentTimeMillis() - startTime) < time) {  
  27.         }  
  28.     }  
  29.       
  30.     /** 
  31.      * @param args the command line arguments 
  32.      */  
  33.     public static void main(String[] args) throws InterruptedException {  
  34.         lineGraph(0.5);  
  35.         sinGraph();  
  36.     }  
  37.       
  38. }  

 

 

package cglib;

 

public class jiekou {

    public static void drawLine(){    
        int usage = 700;  
        System.out.println("Test Begins...");         
        while(true){          
            long start = System.currentTimeMillis();  
            while(System.currentTimeMillis() - start < usage );  
            try{  
                Thread.sleep(1000-usage);  
            }catch(Exception e){  
                System.out.print(e);  
            }         
        }  
    }  
    public static void drawSin(){  
        double x = Math.PI / 2;   
        while(true){  
            //下面这一句+1是因为sinx可能为负数,最大为-1,加上1的话就保证为正了  
            //*0.5是应为加1之后,最大数可能达到2,为了限制在1以内,所以*0.5  
            long usage = (long)((Math.sin(x)+1)*0.5*1000);  
            System.out.println(usage);  
            long start =  System.currentTimeMillis();  
            while(System.currentTimeMillis() - start < usage);  
            try{  
                Thread.sleep(1000 - usage);  
            }catch(Exception e){  
                System.out.print(e);  
            }  
            x += 0.1;  
        }  
    }     
    public static void drawSinSpeedup(){  
        double x = Math.PI / 2;   
        //加入了刷新时间,可以调控曲线弯曲程度  
        int flushtime = 5000;  
        while(true){  
            long usage = (long)((Math.sin(x)+1)*0.5*flushtime);  
            System.out.println(usage);  
            long start =  System.currentTimeMillis();  
            while(System.currentTimeMillis() - start < usage);  
            try{  
                Thread.sleep(flushtime - usage);  
            }catch(Exception e){  
                System.out.print(e);  
            }  
            x += 0.1;  
        }  
    }  
    
    public static void main(String[] args) throws InterruptedException {  
        drawLine();  
        drawSin();  
    }  

    }

转载于:https://my.oschina.net/u/2822116/blog/723882

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值