Copy Code code as follows:
/**
* Gets the number of cores available in this device, across all processors.
* Requires:ability to peruse the filesystem at "/SYS/DEVICES/SYSTEM/CPU"
* @return The number of cores, or 1 if failed to get result
*/
private int getnumcores () {
Private Class to display only CPU devices in the directory listing
Class Cpufilter implements FileFilter {
@Override
Public Boolean accept (File pathname) {
Check if filename is ' CPU ', followed by a single digit number
if (Pattern.matches ("cpu[0-9]", Pathname.getname ())) {
return true;
}
return false;
}
}
try {
Get directory containing CPU info
File dir = new file ("/sys/devices/system/cpu/");
Filter to only list the devices we care about
file[] files = dir.listfiles (new Cpufilter ());
Return the number of cores (Virtual CPU devices)
return files.length;
catch (Exception e) {
Default to return 1 core
return 1;
}
}