public static int getNumCores ( ) { class CpuFilter implements FileFilter { @ Override public boolean accept ( File pathname ) { return Pattern . matches ( "cpu[0-9]" , pathname . getName ( ) ) ; } } if ( sCoreNum == 0 ) { try { // Get directory containing CPU info File dir = new File ( "/sys/devices/system/cpu/" ) ; // Filter to only list the devices we care about File [ ] files = dir . listFiles ( new CpuFilter ( ) ) ; // Return the number of cores (virtual CPU devices) sCoreNum = files . length ; } catch ( Exception e ) { Log . e ( TAG , "getNumCores exception" , e ) ; sCoreNum = 1 ; } } return sCoreNum ; }
Get cpu core number