Skip to content


Number of processors on a machine

Posted in Java, Technology.

How to get Number of processors on a machine in java

public class GetNoOfProcessors
{
public static void main(String[] args)
{
System.out.println(“No. of processors found in the system: ” + getNoOfProcessors());
}

/**
* Gets the no. of processors.
* @return The no. of processors
*/
public static int getNoOfProcessors()
{
int noOfProcessors = 0;
try
{
Runtime runtime = Runtime.getRuntime();
Class runtimeClass = runtime.getClass();
java.lang.reflect.Method availProcessorsMethod = runtimeClass.getMethod(“availableProcessors”, null);
noOfProcessors = ((Integer)availProcessorsMethod.invoke(runtime, null)).intValue();
}
catch (Throwable e)
{
// Ignore this exception as this method is only supported in JDK > 1.4
}
return noOfProcessors;
}
}

Note from Author :
I maintain my website, write articles and reply comments because of my interest/hobby to share information and knowledge.

If you liked the post, Please
Follow me on twitter: http://twitter.com/pankajbatracom
Join My facebook page: http://www.facebook.com/pankajbatra.blog
Join me on LinkedIn: http://www.linkedin.com/in/batrapankaj
Or subscribe to updates by Email by clicking here

To subscribe for updates, Enter your email address:

Related posts:

  1. Facebook Email in response to Google Gmail Buzz
  2. Class Loading in Java
  3. Email Marketing Tool Application
  4. Covariant return types
  5. Java Interview questions


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.







Some HTML is OK

or, reply to this post via trackback.