Home arrow static arrow Java Programming [Archive] - Counting number of processor?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Counting number of processor?
This topic has 2 replies on 1 page.

Registered: 4/4/01
Counting number of processor?  
Jun 18, 2004 2:04 AM


Can I count number of CPU using java? Is there any library which has this ability? Thank you very much in advance.

Tinnapat C.

Registered: 13.8.2003
Re: Counting number of processor?  
Jun 18, 2004 2:20 AM (reply 1 of 2)

At least not in a platform independent manner.

Registered: 18/06/98
Re: Counting number of processor?  
Jun 18, 2004 6:32 AM (reply 2 of 2)

In the forthcoming version of J2SDK 1.5 there will be a package named java.lang.management, and an interface called OperatingSystemMXBean. One of its methods returns the number of CPUs available to the instance of the JVM running.

It depends on the processor count reported by the operating system. If you have a Pentium 4 with hyperthreading enabled your operating system reports 2 "logical" CPUs although you know that you have just only a physical CPU chip in your machine. As I haven't tested the "getAvailableProcessors" method in a HT machine I can't say exactly the result, if the getAvailableProcessors returns the number of logical CPUs or physical CPUs.

(Obviously if you have a machine with 32 CPUs but you're running in a "domain" or a "virtual machine" that do not expose all CPUs to your process, or if some of the CPUs are disabled (it's a common fact among IBM server machines - the CPU chips are physically present, but don't work - just pay for the additional CPUs and they will unlock them), the JVM will report a smaller number. It depends in the hardware.)

public interface OperatingSystemMXBeanThe management interface for the operating system on which the Java virtual machine is running.

A Java virtual machine has a single instance of the implementation class of this interface. This instance implementing this interface is an MXBean that can be obtained by calling the ManagementFactory.getOperatingSystemMXBean() method or from the platform MBeanServer method.
int getAvailableProcessors()
Returns the number of processors available to the Java virtual machine.
This topic has 2 replies on 1 page.