如何从Java代码中找到应用程序可用的内核数量?
#1楼
在安装Cygwin的Windows上可以使用:
System.getenv("NUMBER_OF_PROCESSORS")
#2楼
如果要获取物理核的数量,可以运行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};