为了定时测试服务器性能,需要设计一个跑死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 ();
}
}
}