7

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?

Ryan Dougherty
  • 528
  • 6
  • 21
  • 2
    HT is Processor feature, and OS will use it. Java can't affect it. If you wan't to use all 8 processors, then your code should use all of them by creating 8 threads\etc – Sergey Litvinov Jul 30 '14 at 15:58
  • 1
    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. – Chris K Jul 30 '14 at 15:59
  • That's the job of the OS and/or the machine. If they don't enable it, Java won't provide any ability to. (And if they do, Java should take advantage of it automatically.) – cHao Jul 30 '14 at 15:59
  • http://stackoverflow.com/questions/11738133/is-it-possible-to-check-in-java-if-the-cpu-is-hyper-threading might help – Doon Jul 30 '14 at 15:59
  • 1
    if your CPU is 100% then you probably have a coding issue. – T McKeown Jul 30 '14 at 16:00
  • 1
    Thanks for all of the great answers, everyone. – Ryan Dougherty Jul 30 '14 at 16:01

2 Answers2

8

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

Chris K
  • 11,622
  • 1
  • 36
  • 49
  • 2
    We have run extensive performance analysis of our home-grown java applications. I was surprised to find that we get better performance in many of our jvm's that are 'real-time' (high throughput) and thread-sensitive with hyperthreading *disabled* than when enabled. I suspect this is because java is told that there are threads available, but end up getting delayed because the operations couldn't be pipelined as well as the hyperthreading marketing department hoped. In sum, if you're doing high throughput applications, you may try testing if hyperthreading is actually beneficial or not. – pozcircuitboy Jun 13 '17 at 22:43
3

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.

Saurabh
  • 700
  • 8
  • 6