Open In App

Java Runtime availableProcessors() Method with Examples

Last Updated : 13 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In the Runtime class of Java, there is a method availableProcess(). This method basically returns the number of processors available on the machine during runtime. It returns an integer value representing the number of processor cores used by Java Virtual Machine (JVM). This value is always 1 or greater than 1 because many modern-day computers have at least one processor core. It can be used to get familiar with the hardware capabilities of the system. Unlike most of the methods, this method does not require any parameters.

Syntax of Method:

Runtime.getRuntime().availableProcessors()

Examples of availableProcessors() Method

Example 1 :

Below is the implementation of Java Runtime availableProcessors() Method:

Java




// Java Progra to implement
// Java Runtime availableProcessors() Method
import java.io.*;
  
// Driver Class
class GFG
{
    // main function
    public static void main(String[] args){
        
        //invoing method to get the value
        int avpro = Runtime.getRuntime().availableProcessors();
          
        //Displaying the value stored in variable "avpro"
        System.out.println("Available Processors : "+avpro);
    }
}


Output

Available Processors : 4


In this example we have invoked availableProcessors() method. From this example, we can clearly see that there are 4 processor cores present in the machine at runtime. It also indicates this currently working machine has a quad-core processor.

Example 2:

Below is the implementation of Java Runtime availableProcessors() Method:

Java




// Java Program demonstrate
// Java Runtime availableProcessors() Method
import java.io.*;
  
// Driver Class
class GFG {
    // Creating the multithread function
    public static void multithread(int avpro){
        int i;
        for (i = 1; i <= avpro; i++) {
            // Creating an object of thread class
            Thread obj = new Thread(() -> {
                // Displaying the name nad id of current
                // thread
                System.out.println(
                    "Thread running with Id: "
                    + Thread.currentThread().getId()
                    + " and Name: "
                    + Thread.currentThread().getName());
            });
  
            // starting the new thread
            obj.start();
        }
    }
  
    // Main Function
    public static void main(String[] args){
  
        // getting available current processors core
        int avpro = Runtime.getRuntime().availableProcessors();
  
        // invoking the created multithread method
        multithread(avpro);
    }
}


Output

Thread running with Id: 13 and Name: Thread-2
Thread running with Id: 11 and Name: Thread-0
Thread running with Id: 14 and Name: Thread-3
Thread running with Id: 12 and Name: Thread-1


We can use Runtime.getRuntime().availableProcessors() method to get the current available processors cores. This will help us in multiprocessing. When our program is finished, we must shut down thread pools otherwise the thread pool threads prevent the program from terminating. It we exceed the range of available processors it simply won’t terminate the program.

Conclusion

The Runtime java provides us the access to runtime environment. It has availableProcessors() method that return an integer value representing the number of currently available processors core during runtime. This Method is has characteristics:

  • It never return value > 1 as many modern days computer have at least one processor core.
  • It value may differ for different machine depending on the number of processor core during runtime.

In this article we had seen deep implement of availableProcessors() method with suitable and efficient examples. We have tried to cover all the points regarding the method.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads