Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding Number of Cores in Java

Tags:

java

How can I find the number of cores available to my application from within Java code?

like image 842
Damir Avatar asked Jan 21 '11 13:01

Damir


People also ask

How do I check the number of cores?

Press Ctrl + Shift + Esc to open Task Manager. Select the Performance tab to see how many cores and logical processors your PC has.

How many threads does a core Java have?

A single CPU core can have up-to 2 threads per core. For example, if a CPU is dual core (i.e., 2 cores) it will have 4 threads.

What is number of cores in server?

The number of cores is a physical property of the server and is designed in. Processors have been built with 1, 2, 4, and 8 cores commonly. You can find the number of cores by consulting the manufacturers' specs.


2 Answers

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

If cores is less than one, either your processor is about to die, or your JVM has a serious bug in it, or the universe is about to blow up.

like image 100
darioo Avatar answered Sep 22 '22 04:09

darioo


If you want to get number of physical cores you can run cmd and terminal command and then to parse the output to get info you need.Below is shown function that returns number of physical cores .

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 class:

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

}

like image 29
adi9090 Avatar answered Sep 20 '22 04:09

adi9090