Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java determine number of cpu cores

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

java determine number of cpu 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;
}
Comment

PREVIOUS NEXT
Code Example
Java :: How to perform an iterative inorder traversal through a binary search tree, in Java? 
Java :: java integer division to float 
Java :: java array merge 
Java :: mod 10e9+7 in java 
Java :: javac encoding utf-8 
Java :: java empty arraylist 
Java :: org.springframework.security.oauth2.jwt.JwtEncoder 
Java :: java array object 
Java :: create a border java 
Java :: java regex for ip address 
Java :: arraylist in java 
Java :: java program to convert milliseconds to minutes and seconds 
Java :: next method jdbc 
Java :: Java Creating a TreeMap 
Java :: array copy java 
Java :: share location in android programmatically 
Java :: final keyword in java 
Java :: Java read in 
Java :: sharedpreferences 
Java :: 2-dimensional Array java 
Java :: random number 
Java :: lambda function java 
Java :: Java Remove ArrayList Elements 
Java :: json java 
Java :: how to start array index from 1 in java 
Java :: Manual Custom Queries in spring boot 
Java :: stream reduce 
Java :: new int[] java 
Java :: c++ vs java 
Java :: generics in java 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =