有一个测试需求,在电脑的CPU和内存达到90%以上的时候去测试一个应用程序是否可以正常使用。所以写一个死循环去把CPU和内存打满。把代码分享给大家,直接拿去用就行了。
public static void main( String[] args ) {
Vector v = new Vector();
while (true){
byte b[] = new byte[10240*5];
v.add(b);
new Thread(new Runnable() {
@Override
public void run() {
int busyTime = 10;
int idleTime = busyTime;
long startTime = 0;
while (true){
startTime = System.currentTimeMillis();
System.out.println(System.currentTimeMillis()+","+startTime+","+(System.currentTimeMillis() - startTime));
while ((System.currentTimeMillis() - startTime) <= busyTime);
try {
Thread.sleep(idleTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
该博客提供了一段Java代码,用于创建一个死循环,使电脑的CPU和内存占用率达到90%以上,以此来测试应用程序在高负载条件下的运行情况。通过启动多个线程并持续消耗资源,该方法能够模拟系统压力,检查目标程序在极端条件下的性能和稳定性。

2937

被折叠的 条评论
为什么被折叠?



