Runtime

Get number of processors with Runtime

In this example we shall show you how to get the number of processors with Runtime class. Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method. An application cannot create its own instance of this class. To get the number of processors with Runtime class one should perform the following steps:

  • Use getRuntime() API method of Runtime. This method returns the runtime object associated with the current Java application.
  • Use availableProcessors() API method of Runtime. This method returns the number of processors available to the Java virtual machine.,

as described in the code snippet below.

package com.javacodegeeks.snippets.core;

public class GetNumberOfProcessorsWithRuntime {
	
	public static void main(String[] args) {
		
		// get the runtime object associated with the current Java application
		Runtime runtime = Runtime.getRuntime();
		
		// get the number of processors available to the Java virtual machine
		int numberOfProcessors = runtime.availableProcessors();
		 
		System.out.println("Number of processors available to this JVM: " + numberOfProcessors);
		
	}

}

 
This was an example of how to get the number of processors with Runtime class in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
boojae
boojae
6 years ago

hi… first of all thanks for this post
How can I print Hello World from every processors?

Back to top button