Wednesday, September 6, 2017

Increase CPU by Java Example

Increase CPU Load by Java Example

1) Will take 25% CPU in Eclipse, Run 4 instances for 100% CPU.
package utility;
public class BumpCPU {
    public static void main(String[] args) {
        for(; ;) {
        }
    }
}

2) Keep all the Processors in System busy resulting 100% CPU Usage.
package utility;
//Will take 100% CPU, All Processors will be bombarded for creating threads and will be busy by JVM.
public class BurnCPU {
    public static void main(String[] args) {
        int count = Runtime.getRuntime().availableProcessors();
        System.out.println("No of Processors: "+count);
        
        for(int i = 0; i < count; i++) {
            new Thread(new Runnable() {
                public void run() {
                    while(true);  //Thread keep running.
                }
            }).start();
        }
    }
}

------Run above programs and check "top" command output------

No comments:

Post a Comment