Runtime#availableProcessors() doesn't return correct result on Linux server

我的梦境 提交于 2019-12-25 07:03:01

问题


I usually run Runtime#availableProcessors to determine how many cores on a Windows computer and it works fine. The result is consistent with that I found from control panel.

However when I applied the API on a Linux server, it returns 1. As I know the server is more powerful it doesn't make sense to me it's a single cpu system.

I did some search and found the Linux box is Intel(R) Xeon(R) CPU X5675 @ 3.07GHz, googling shows it has 6 cpu cores.

Then the question is, why Runtime#availableProcessors misreported? Is it a bug?

Thanks,

John

Here is the entire output of /proc/cpuinfo

processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 44
model name      : Intel(R) Xeon(R) CPU           X5675  @ 3.07GHz
stepping        : 2
cpu MHz         : 3059.000
cache size      : 12288 KB
fpu             : yes
fpu_exception   : yes
cpuid level     : 11
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc up ida nonstop_tsc arat pni ssse3 cx16 sse4_1 sse4_2 popcnt lahf_lm
bogomips        : 6118.00
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management: [8]

回答1:


It must be how your server is configured (perhaps you're running in a VM). When I run, on my personal Linux laptop,

public static void main(String[] args) {
    System.out.println(Runtime.getRuntime().availableProcessors());
}

I get

4

as this machine has four cores. And cat /proc/cpuinfo reports the same.



来源:https://stackoverflow.com/questions/27573347/runtimeavailableprocessors-doesnt-return-correct-result-on-linux-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!