Java利用PI计算做一个烧CPU的功能

为了定时测试服务器性能,需要设计一个跑死CPU的功能

计划使用计算PI的方法,然后创建多个线程,同时去跑PI方法,当到了指定时间就开始执行,执行完之前,不做其他操作。没有到指定时间,则进行休息

package com.company;

import com.company.util.StringUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by user on 2021/12/22.
 */
public class RunCPU {

    public static void autoRun(String week,String time,String thread_count,String pi_num){
        //System.out.println(rand_pi(1000000000)); //改变参数值
        SimpleDateFormat myFmt3 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss E");

        if(week==null||week.trim().equals("")){
            week = "三";
        }
        if(time==null||time.trim().equals("")){
            time = " 18:";
        }
        if(thread_count==null||thread_count.trim().equals("")||!StringUtil.isDigit(thread_count)){
            thread_count = "15";
        }
        if(pi_num==null||pi_num.trim().equals("")||!StringUtil.isDigit(pi_num)){
            pi_num = "1000000000";
        }
        int sleep = 10000;
        try {
            while (true) {
                Date now = new Date();
                String today = myFmt3.format(now);
                System.out.println(today);
                if (today.trim().endsWith(week) && today.indexOf(time) > 0) {
                    System.out.println(today);
                    //System.out.println(rand_pi(1000000000));
                    int pro_int = Integer.parseInt(thread_count);


                    ExecutorService exe = Executors.newFixedThreadPool(pro_int);
                    for(int i=0;i<pro_int;i++){
                        long mill =  System.currentTimeMillis();
                        exe.execute( new ThreadDemo( Integer.parseInt(pi_num),"Thread-"+i+"-"+mill));

                        System.out.println("Thread-"+mill+" is start now!");

                    }
                    //该方法在加入线程队列的线程执行完之前不会执行。exe.isTerminated()当shutdown()或者shutdownNow()执行了之后才会执行,并返回true。
                    exe.shutdown();

                    while (true) {
                        if (exe.isTerminated()) {
                            now = new Date();
                            today = myFmt3.format(now);
                            System.out.println(today+"线程都执行结束了!");
                            break;
                        }
                        System.out.println("将休眠5000 Millis!");
                        Thread.sleep(5000);
                    }

                    sleep = 10000;
                }else{
                    sleep = (10000+sleep)%60000;
                    System.out.println(today+"today.trim().endsWith("+week+") && today.indexOf("+time+") 匹配不到, 休眠 "+sleep+" Millsecond");
                }
                try {
                    System.out.println("将休眠 "+sleep+" Millsecond");
                    Thread.sleep(sleep);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
// TODO Auto-generated method stub
        autoRun(null,null,"3","1000");

    }

    public  double rand_pi(int n,String name) {
        int numInCircle = 0;

        double x, y;

        double pi;
        System.out.println(name+" computer start  "+numInCircle+ " !");
        for(int i=0;i < n; i++){
            if( i%(n/10)==0) {
                System.out.println(name + " start " + i + " rand_pi computer");
            }
            synchronized(this) {
                x = Math.random();

                y = Math.random();
            }
            if( i%(n/10)==0) {
                System.out.println(name + "  start " + i + "  rand_pi computer");
            }
            if(x * x + y * y < 1)

                numInCircle++;

        }
        System.out.println(name+" computer end  "+numInCircle+ " !");

        pi=(4.0 * numInCircle) / n;

        return pi;

    }
}


 class ThreadDemo extends Thread {
    private Thread t;
    private String threadName;
    private int num = 1000000;

    ThreadDemo( int num ,String name) {
        threadName = name;
        this.num = num;
        System.out.println("Creating " +  threadName );
    }

    public void run() {
        System.out.println("Running " +  threadName );
        try {
            /*for(int i = 4; i > 0; i--) {
                System.out.println("Thread: " + threadName + ", " + i);
                // 让线程睡眠一会
                Thread.sleep(50);
            }*/
            RunCPU r = new RunCPU();
            r.rand_pi(this.num,threadName);
        }catch (Exception e) {
            System.out.println("Thread " +  threadName + " interrupted.");
        }
        System.out.println("Thread " +  threadName + " exiting.");
    }

    public void start () {
        System.out.println("Starting " +  threadName );
        if (t == null) {
            t = new Thread (this, threadName);
            t.start ();
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xcl119xxcl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值