Get number of available processors in Java


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

Updated on: 25-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements