Tuesday, March 17, 2015

Java code to get number of processors available

Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime() method.

availableProcessors() method of Runtime returns the number of processors available to the Java virtual machine.

This value may change during a particular invocation of the virtual machine. Applications that are sensitive to the number of available processors should therefore occasionally poll this property and adjust their resource usage appropriately.

Example:
package java_availableprocessors;

public class Java_availableProcessors {

    public static void main(String[] args) {
        System.out.print("availableProcessors = " 
            + Runtime.getRuntime().availableProcessors() + "\n");
    }
    
}


No comments:

Post a Comment