《編程之美》(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;.net

 

public class jiekou {get

    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();  
    }  io

    }class

相關文章
相關標籤/搜索