Get number of available processors in Java

Java 8Object Oriented ProgrammingProgramming

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 −

Example

 Live Demo

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());
   }
}

Output

Start...
Number of available processors are: 32
raja
Published on 08-Jan-2019 16:34:59
Advertisements