Java Runtime availableProcessors() Method with Examples
Last Updated :
13 Nov, 2023
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
import java.io.*;
class GFG
{
public static void main(String[] args){
int avpro = Runtime.getRuntime().availableProcessors();
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
import java.io.*;
class GFG {
public static void multithread( int avpro){
int i;
for (i = 1 ; i <= avpro; i++) {
Thread obj = new Thread(() -> {
System.out.println(
"Thread running with Id: "
+ Thread.currentThread().getId()
+ " and Name: "
+ Thread.currentThread().getName());
});
obj.start();
}
}
public static void main(String[] args){
int avpro = Runtime.getRuntime().availableProcessors();
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.
Share your thoughts in the comments
Please Login to comment...