availableProcessors() returns 1 for dualcore phones

我的未来我决定 提交于 2019-12-05 01:11:48

Runtime.getRuntime().availableProcessors() only returns the number of online processors, so it will return 1 when the second core is sleeping. This is done in order to preserve power during less resource-intensive tasks.

To see all available cores, look at /sys/devices/system/cpu/

android developer

This will get you the number of cores on Android (based on this post) :

public static int getCoresCount()
    {
    class CpuFilter implements FileFilter
      {
      @Override
      public boolean accept(final File pathname)
        {
        if(Pattern.matches("cpu[0-9]+",pathname.getName()))
          return true;
        return false;
        }
      }
    try
      {
      final File dir=new File("/sys/devices/system/cpu/");
      final File[] files=dir.listFiles(new CpuFilter());
      return files.length;
      }
    catch(final Exception e)
      {
      return Math.max(1,Runtime.getRuntime().availableProcessors());
      }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!