Java examples for Native OS:CPU
Returns number of CPUs in the Linus system
//package com.java2s; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Main { private static final String CPU_INFO = "proc/cpuinfo"; private static final String ID_PROCESSOR = "processor"; private static Integer cpu_count = null; /**// w w w . j av a2s. c om * Returns number of CPUs in the system * @return number of CPUs in the system */ public static final int getCpuCount() { if (cpu_count == null) { innerGetCpuCount(); } return cpu_count; } private static void innerGetCpuCount() { cpu_count = 0; BufferedReader br = null; try { br = new BufferedReader(new FileReader(CPU_INFO)); String line; while ((line = br.readLine()) != null) { if (line.toLowerCase().contains(ID_PROCESSOR)) { cpu_count++; } } } catch (Exception e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } }