JAVA实现对于多核CPU的OS满足CPU使用率在50%左右以及实现CPU使用率为正弦曲线

博客介绍了如何在JAVA中实现多核CPU的利用率保持在50%,以及如何让CPU使用率呈现正弦曲线。作者参考了《编程之美》并探讨了在单核和多核环境下如何控制CPU占用率,提供了相应的Java代码实现,并展示了执行效果。

参考自一位大牛:

https://blog.csdn.net/java2000_net/article/details/3136124

前言

今天看了下《编程之美》的第一章 第一节 让CPU占用率曲线听你指挥,感觉很有意思。在网上找了很多大牛写的方法,但是没找到详细介绍JAVA怎么在多核OS中实现这样效果的。

思考

对于单核CPU的OS,网上提供了很多解法。如下代码:

public static void main(String args[]) throws InterruptedException{
            int busyTime = 50;//可调节参数,单位为ms。50ms后线程休眠50毫秒,然后再经系统调度
            int idleTime = busyTime;
            while(true){
                long startTime = System.currentTimeMillis();
                //busy loop:
                while((System.currentTimeMillis()-startTime)<=busyTime)
                    ;
                try {
                    Thread.sleep(idleTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
}

若要在多核CPU的OS也满足 CPU使用率停留在50%左右,那么只需有多少个核,则开启多少个线程执行上述操作。(每一个CPU(其实是每核),在同一时间,只工作一条线程)。因此,只需获得当前系统的CPU核数,再创建多个线程即可

Java实现多核CPU满足50%CPU使用率

实现代码如下:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test {
    public static void main(String args[]) throws InterruptedException{
        Runtime r = Runtime.getRuntime();//获得当前系统的CPU数量,根据这个数值创建对应数量的线程
        ExecutorService pool = Executors.newFixedThreadPool(r.availableProcessors());
        for (int i = 0;i < r.availableProcessors();i++){
            pool.execute(new Loop());
        }
        pool.shutdown();
    }
}
class Loop implements Runnable{
    @Override
    public void run() {
            int busyTime = 50;//可调节参数,单位为ms。50ms后线程休眠50毫秒,然后再经系统调度。该数值越小,则线程被调度得越频繁,则CPU使用率也就越高(平均)
            int idleTime = busyTime;
            while(true){
                long startTime = System.currentTimeMillis();
                //busy loop:
                while((System.currentTimeMillis()-startTime)<=busyTime)
                    ;
                try {
                    Thread.sleep(idleTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    }
}

效果图如下:(将所有CPU一张表显示)

Java实现多核CPU使用率为正弦曲线

代码及个人解释如下:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test {
     public static void main(String[] args) throws Exception {
         Runtime r = Runtime.getRuntime();//获得当前系统的CPU数量,根据这个数值创建对应数量的线程
         ExecutorService pool = Executors.newFixedThreadPool(r.availableProcessors());
         for (int i = 0;i < r.availableProcessors();i++){
             pool.execute(new Loop());
         }
         pool.shutdown();
    }
}
class Loop implements Runnable{
    @Override
    public void run() {
        /*
            int busyTime = 50;//可调节参数,单位为ms。50ms后线程休眠50毫秒,然后再经系统调度
            int idleTime = busyTime ;
            while(true){
                long startTime = System.currentTimeMillis();
                //busy loop:
                while((System.currentTimeMillis()-startTime)<=busyTime)
                    ;
                try {
                    Thread.sleep(idleTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            */
        // 角度的分割
        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;
        /*对busySpan和idleSpan数组赋值
        busySpan:100 102 104 ... 200 198 196 ... 100  98  96 ...   0   2   4 ... 100 循环
        idleSpan: 100  98  96 ...   0   2   4 ... 100 102 104 ... 200 198 196 ... 100 循环
        对于CPU,每次先工作busySpan[i]ms,再休眠idleSpan[i]ms。一次循环分四个阶段解读:
        阶段一:busySpan : 100 -> 200 idleSpan: 100 -> 0 在此阶段,CPU利用率整体呈上升(busySpan[i] - idleSpan[i] >= 0),
                且每经过一个busySpan[i]和idleSpan[i]之后,曲线上升的幅度相比前一次更大,因为busySpan[i] - idleSpan[i]
                递增
        阶段二:busySpan: 200 -> 100 idleSpan: 0 -> 100 分析同上,此阶段CPU利用率整体仍呈上升趋势,但每一次上升幅度比前
                一次上升幅度小
        阶段三:busySpan: 100 -> 0 idleSpan: 100 -> 200 在此阶段,CPU利用率呈下降趋势,且下降幅度越来越大
        阶段四:busySpan: 0 -> 100 idleSpan: 200 -> 100 在此阶段,CPU利用率继续呈下降趋势,下降幅度越来越小
        最后,循环上述四个阶段
        */
        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) {
                e.printStackTrace();
            }
            j++;
        }
    }
}

效果图如下:

 

 

 

 

 

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值