Java写一个死循环打满CPU和内存

该博客提供了一段Java代码,用于创建一个死循环,使电脑的CPU和内存占用率达到90%以上,以此来测试应用程序在高负载条件下的运行情况。通过启动多个线程并持续消耗资源,该方法能够模拟系统压力,检查目标程序在极端条件下的性能和稳定性。
摘要由CSDN通过智能技术生成

有一个测试需求,在电脑的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();
    }
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值