मैं जावा कोड के भीतर अपने आवेदन के लिए उपलब्ध कोर की संख्या कैसे पा सकता हूं?
मैं जावा कोड के भीतर अपने आवेदन के लिए उपलब्ध कोर की संख्या कैसे पा सकता हूं?
जवाबों:
int cores = Runtime.getRuntime().availableProcessors();
अगर cores एक से कम है, तो या तो आपका प्रोसेसर मरने वाला है, या आपके JVM में एक गंभीर बग है, या ब्रह्मांड उड़ने वाला है।
यदि आप भौतिक कोर की संख्या प्राप्त करना चाहते हैं, तो आप cmd और टर्मिनल कमांड को चला सकते हैं और फिर आउटपुट को पार्स करने के लिए अपनी जरूरत की जानकारी प्राप्त कर सकते हैं। बेलो को फंक्शन दिखाया गया है जो भौतिक कोर की संख्या देता है।
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";
}
}
}
यह सीपीयू कोर की संख्या (और बहुत सारी अन्य जानकारी) का पता लगाने का एक अतिरिक्त तरीका है, लेकिन इस कोड के लिए अतिरिक्त निर्भरता की आवश्यकता है:
नेटिव ऑपरेटिंग सिस्टम और हार्डवेयर जानकारी https://github.com/oshi/oshi
SystemInfo systemInfo = new SystemInfo();
HardwareAbstractionLayer hardwareAbstractionLayer = systemInfo.getHardware();
CentralProcessor centralProcessor = hardwareAbstractionLayer.getProcessor();
प्रसंस्करण के लिए उपलब्ध तार्किक सीपीयू की संख्या प्राप्त करें:
centralProcessor.getLogicalProcessorCount();
यह सिगविन के साथ विंडोज पर काम करता है:
System.getenv("NUMBER_OF_PROCESSORS")
groovy -e "println System.getenv('NUMBER_OF_PROCESSORS')"
set NUMBER_OF_PROCESSORSमेरे लिए विंडोज कमांड लाइन से काम करता है।