Runtime.availableProcessors();
public int availableProcessors() {
return (int) Libcore.os.sysconf(_SC_NPROCESSORS_CONF);
}
You can reference the source code from https://cs.android.com.
int __get_cpu_count(const char* sys_file) {
int cpu_count = 1;
FILE* fp = fopen(sys_file, "re");
if (fp != nullptr) {
char* line = nullptr;
size_t allocated_size = 0;
if (getline(&line, &allocated_size, fp) != -1) {
cpu_count = GetCpuCountFromString(line);
}
free(line);
fclose(fp);
}
return cpu_count;
}
int get_nprocs_conf() {
// It's unclear to me whether this is intended to be "possible" or "present",
// but on mobile they're unlikely to differ.
return __get_cpu_count("/sys/devices/system/cpu/possible");
}
int get_nprocs() {
return __get_cpu_count("/sys/devices/system/cpu/online");
}