2013-12-08 108 views
0

我们都知道Android操作系统支持多核心架构。但是,如何测试这些线程实际上是在多个内核上进行处理的?它是一个项目分配。请建议如何继续。如何查看Android操作系统真的使用多核心

public int numCores() throws IOException { 
    Pattern processorLinePattern = Pattern.compile("^processor\\s+: \\d+$", Pattern.MULTILINE); 
    String cpuinfo = Files.toString(new File("/proc/cpuinfo"), Charsets.US_ASCII); 
    Matcher matcher = processorLinePattern.matcher(cpuinfo); 
    int count = 0; 
    while (matcher.find()) { 
     count++; 
    } 
    return count; 
} 

通常你应该喜欢做与像番石榴或Apache下议院助手库I/O,有很多优势的情况下获得:

回答

1
try this.Might help you 

private int getNumCores() { 
    //Private Class to display only CPU devices in the directory listing 
    class CpuFilter implements FileFilter { 
     @Override 
     public boolean accept(File pathname) { 
      //Check if filename is "cpu", followed by a single digit number 
      if(Pattern.matches("cpu[0-9]+", pathname.getName())) { 
       return true; 
      } 
      return false; 
     }  
    } 

    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) 
     return files.length; 
    } catch(Exception e) { 
     //Default to return 1 core 
     return 1; 
    } 
} 
0

使用Guava's Files helper读取文件试试parsing /proc/cpuinfo,例如对。如果你需要避免依赖,自己实现类似的东西(这个示例实现有几个漏洞,但并不太可怕):

private String readFile(File file, Charset charset) throws IOException { 
    StringBuilder result = new StringBuilder(); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset)); 
    try { 
     String line; 
     while ((line = reader.readLine()) != null) { 
      result.append(line).append("\n"); 
     } 
     return result.toString(); 
    } finally { 
     reader.close(); 
    } 
} 

public int numCores() throws IOException { 
    // ... 
    String cpuinfo = readFile(new File("/proc/cpuinfo"), Charset.forName("US-ASCII")); 
    // ... 
}