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

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