get Number of CPU Cores - Android Hardware

Android examples for Hardware:CPU Information

Description

get Number of CPU Cores

Demo Code


//package com.java2s;

import java.io.File;
import java.io.FileFilter;

import java.util.regex.Pattern;

public class Main {

    public static int getNumCores() {
        try {/*from w  w w. j  a v a  2 s.  c om*/
            File dir = new File("/sys/devices/system/cpu/");
            File[] files = dir.listFiles(new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    if (Pattern.matches("cpu[0-9]", pathname.getName())) {
                        return true;
                    }
                    return false;
                }
            });
            return files.length;
        } catch (Exception e) {
            return 1;
        }
    }
}

Related Tutorials