日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java取内核数_在Java中查找内核数

發布時間:2024/9/19 java 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java取内核数_在Java中查找内核数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

如何從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};

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.indexOf("win") >= 0);

}

public static boolean isMac() {

return (OS.indexOf("mac") >= 0);

}

public static boolean isUnix() {

return (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0 );

}

public static boolean isSolaris() {

return (OS.indexOf("sunos") >= 0);

}

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";

}

}

}

#3樓

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

如果cores少于一個,則可能是處理器即將死機,或者JVM中存在嚴重的錯誤,或者Universe即將崩潰。

#4樓

這是找出CPU內核數量(以及許多其他信息)的另一種方法,但是此代碼需要附加的依賴關系:

SystemInfo systemInfo = new SystemInfo();

HardwareAbstractionLayer hardwareAbstractionLayer = systemInfo.getHardware();

CentralProcessor centralProcessor = hardwareAbstractionLayer.getProcessor();

獲取可用于處理的邏輯CPU的數量:

centralProcessor.getLogicalProcessorCount();

#5樓

public class CPUCores {

public static void main(String[] args) {

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

System.out.println("CPU cores: " + processors);

}

}

輸出CPU內核:8

請注意,此數字是可用于Java應用程序的內核總數。

參考Java Doc API

超線程

總結

以上是生活随笔為你收集整理的java取内核数_在Java中查找内核数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。