I have a multithreaded program running on a quad-core Intel i7. When I execute Runtime.getRuntime.availableProcessors()
, I get 8, and I know that hyperthreading is available on this CPU.
However, when I create threads, my CPU levels are at 100% (i.e. non-zero) for 4 threads, meaning that 4 threads are unused. Is there any way to enable hyperthreading in Java?
Hyperthreading is enabled by Java as it uses native threads, thus this is a OS/CPU config. However Hyperthreading does not give you extra cores, it permits timeshare of the four cpus that you have. If you have maxed out the four cpus with four threads, then that is possible with hyperthreading turned on or off.
If you want to make sure hyperthreading is worth the increased performance, run your system in BIOS and check how much temperature it generates compared to when it's disabled. Then, you should decide if you can reduce the added temperature by using a better thermal paste. If not, you may want to disable the feature.
Hyperthreading is enabled by the fact that all modern JVMs use native threads, thus this is a OS/CPU config setting.
However Hyperthreading does not give you extra cores, it permits fine grained timeshare of the four cpus that you have. That is, while one thread is stalled, say waiting for a page of memory to be flown into the cache then another thread can swoop in and make use of parts of the CPU. It adds about 10% extra to the size of a CPU core due to more complex scheduling requirements and does not benefit all applications.
If you have maxed out the four cpus with four threads, then that is possible with hyperthreading turned on or off. It just means that those threads are running hot, without blocking much.
The reason that Java reports an 8 core CPU, rather than 4 is because the OS tells Java that the CPU has 8 cores. The OS believes that because the OS has been told to schedule threads as though it was an 8 core CPU, this made adding support of hyperthreading to OSes much simpler. The OS carries on managing threads as before, oblivious to much of the inner workings of hyperthreading and lets the CPU manage the low level scheduling of the assembly as and when parts of the cpu become available.
A more detailed discussion, with benchmarks can be read here
Use the following command
$ lscpu
The output can be used to determine the actual number of cores and if you have hyperthreading enabled or not.
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 32
On-line CPU(s) list: 0-31
Thread(s) per core: 2
Core(s) per socket: 8
Socket(s): 2
NUMA node(s): 2
Vendor ID: GenuineIntel
CPU family: 6
Model: 45
Stepping: 7
CPU MHz: 2399.995
BogoMIPS: 4799.35
Virtualization: VT-x
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 20480K
NUMA node0 CPU(s): 0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30
NUMA node1 CPU(s): 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31
The actual parallelism that can be achieved will be (No. of Sockets) X (Cores per socket) X (Threads per core). To determine if your processor is hyperthreaded or not you can use the Threads per core parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With