欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > java >内容正文

java

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

发布时间:2025/3/19 java 64 豆豆
生活随笔 收集整理的这篇文章主要介绍了 《编程之美》(Java实现) :让CPU占用率画直线和正弦曲线(Java实现) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

2019独角兽企业重金招聘Python工程师标准>>>

  • public class CPUTest {  
  •       
  •     //定义时间片大小(毫秒)  
  •     public static final double TIME = 1000;  
  •     //画直线方法  
  •     private static void lineGraph(double rate) throws InterruptedException{  
  •         while (true){  
  •             doSomeSimpleWork(rate * TIME);  
  •             Thread.sleep((long) (TIME - rate * TIME));  
  •         }  
  •     }  
  •     //画正弦曲线方法  
  •     private static void sinGraph() throws InterruptedException{  
  •         double x = 0;  
  •         double y = 0;         
  •         while (true){  
  •             y = (Math.sin(x) + 1) * TIME / 2;  
  •             doSomeSimpleWork(y);  
  •             x += 0.1;  
  •             Thread.sleep((long) (TIME - y));  
  •         }  
  •     }  
  •     //占用CPU方法  
  •     private static void doSomeSimpleWork(double time) {  
  •         long startTime = System.currentTimeMillis();  
  •         while ((System.currentTimeMillis() - startTime) < time) {  
  •         }  
  •     }  
  •       
  •     /** 
  •      * @param args the command line arguments 
  •      */  
  •     public static void main(String[] args) throws InterruptedException {  
  •         lineGraph(0.5);  
  •         sinGraph();  
  •     }  
  •       
  • }  
  •  

     

    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

    总结

    以上是生活随笔为你收集整理的《编程之美》(Java实现) :让CPU占用率画直线和正弦曲线(Java实现)的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。