In order to get the number of available processors in Java, we use the availableProcessors() method. The java.lang.Runtime.availableProcessors() method returns the number of processors which are available to the Java virtual machine. This number may vary during a particular call of the virtual machine.
Declaration − The java.lang.Runtime.availableProcessors() method is declared as follows −
public int availableProcessors()
Let us see a program to get the number of available processors in Java −
public class Example { public static void main(String[] args) { // print statement at the start of the program System.out.println("Start..."); System.out.print("Number of available processors are: "); System.out.println( Runtime.getRuntime().availableProcessors()); } }
Start... Number of available processors are: 32