-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCPU_Cores_JVM.java
More file actions
47 lines (41 loc) · 1.47 KB
/
CPU_Cores_JVM.java
File metadata and controls
47 lines (41 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.github.os;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* This class CPU_Cores_JVM is to determine the number of processes available to the
* Java Virtual Machine by using the static Runtime method.
* <P> Number of CPU cores allocated to the JVM
* <a href="https://stackoverflow.com/q/4759570/5081877">stackoverflow Post</a>
* @author yashwanth.m
*
*/
public class CPU_Cores_JVM {
public static void main(String[] args) {
int cores = Runtime.getRuntime().availableProcessors();
System.out.println("Allocation CPU Core in Current Machine « "+cores);
System.out.println("CPU Cores : "+ getNumberOfCPUCores() );
//System.out.println("Windows with Cygwin installed : "+ System.getenv("NUMBER_OF_PROCESSORS"));
}
public static int getNumberOfCPUCores() {
String command =
//"cmd.exe /c start WMIC CPU Get /Format:List";
"cmd /C WMIC CPU Get /Format:List";
Process process = null;
int numberOfCores = 0;
try {
process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader( new InputStreamReader(process.getInputStream()) );
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("NumberOfCores")) {
numberOfCores = Integer.parseInt(line.split("=")[1]);
}
System.out.println( line );
}
} catch (IOException e) {
e.printStackTrace();
}
return numberOfCores;
}
}