如何从Java代码中找到应用程序可用的内核数?


int cores = Runtime.getRuntime().availableProcessors();

如果内核数小于1,要么你的处理器就要死了,要么你的JVM有严重的bug,要么宇宙就要爆炸了。


这适用于安装了Cygwin的Windows:

System.getenv(“NUMBER_OF_PROCESSORS”)


如果你想获得物理核的数量,你可以运行cmd和terminal命令,然后解析输出,以获得你需要的信息。下面是返回物理核数的函数。

private int getNumberOfCPUCores() {
    OSValidator osValidator = new OSValidator();
    String command = "";
    if(osValidator.isMac()){
        command = "sysctl -n machdep.cpu.core_count";
    }else if(osValidator.isUnix()){
        command = "lscpu";
    }else if(osValidator.isWindows()){
        command = "cmd /C WMIC CPU Get /Format:List";
    }
    Process process = null;
    int numberOfCores = 0;
    int sockets = 0;
    try {
        if(osValidator.isMac()){
            String[] cmd = { "/bin/sh", "-c", command};
            process = Runtime.getRuntime().exec(cmd);
        }else{
            process = Runtime.getRuntime().exec(command);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    BufferedReader reader = new BufferedReader(
            new InputStreamReader(process.getInputStream()));
    String line;

    try {
        while ((line = reader.readLine()) != null) {
            if(osValidator.isMac()){
                numberOfCores = line.length() > 0 ? Integer.parseInt(line) : 0;
            }else if (osValidator.isUnix()) {
                if (line.contains("Core(s) per socket:")) {
                    numberOfCores = Integer.parseInt(line.split("\\s+")[line.split("\\s+").length - 1]);
                }
                if(line.contains("Socket(s):")){
                    sockets = Integer.parseInt(line.split("\\s+")[line.split("\\s+").length - 1]);
                }
            } else if (osValidator.isWindows()) {
                if (line.contains("NumberOfCores")) {
                    numberOfCores = Integer.parseInt(line.split("=")[1]);
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    if(osValidator.isUnix()){
        return numberOfCores * sockets;
    }
    return numberOfCores;
}

OSValidator经济舱:

public class OSValidator {

private static String OS = System.getProperty("os.name").toLowerCase();

public static void main(String[] args) {

    System.out.println(OS);

    if (isWindows()) {
        System.out.println("This is Windows");
    } else if (isMac()) {
        System.out.println("This is Mac");
    } else if (isUnix()) {
        System.out.println("This is Unix or Linux");
    } else if (isSolaris()) {
        System.out.println("This is Solaris");
    } else {
        System.out.println("Your OS is not support!!");
    }
}

public static boolean isWindows() {
    return (OS.contains("win"));
}

public static boolean isMac() {
    return (OS.contains("mac"));
}

public static boolean isUnix() {
    return (OS.contains("nix") || OS.contains("nux") || OS.contains("aix") );
}

public static boolean isSolaris() {
    return (OS.contains("sunos"));
}
public static String getOS(){
    if (isWindows()) {
        return "win";
    } else if (isMac()) {
        return "osx";
    } else if (isUnix()) {
        return "uni";
    } else if (isSolaris()) {
        return "sol";
    } else {
        return "err";
    }
}

}


这是一种额外的方法来找出CPU核数(以及许多其他信息),但这段代码需要一个额外的依赖:

本机操作系统和硬件信息 https://github.com/oshi/oshi

SystemInfo systemInfo = new SystemInfo();
HardwareAbstractionLayer hardwareAbstractionLayer = systemInfo.getHardware();
CentralProcessor centralProcessor = hardwareAbstractionLayer.getProcessor();

获取可用于处理的逻辑cpu数量:

centralProcessor.getLogicalProcessorCount();

如果你想要检查你机器上的核数和你的java程序给你的核数。

Linux终端:lscpu

在Windows终端(cmd): echo %NUMBER_OF_PROCESSORS%

Mac终端:sysctl -n hww .ncpu