software threads vs hardware threads

Bhadri picture Bhadri · Apr 8, 2011 · Viewed 41.8k times · Source

What is the difference between software threads, hardware threads and java threads?

Are software threads, java threads and hardware threads independent or interdependent? I am asking this because, I know Java threads are created inside a process with in jvm (java.exe).

Also is it true that these different process are executed on different hardware threads?

Answer

mdma picture mdma · Apr 8, 2011

A "hardware thread" is a physical CPU or core. So, a 4 core CPU can genuinely support 4 hardware threads at once - the CPU really is doing 4 things at the same time.

One hardware thread can run many software threads. In modern operating systems, this is often done by time-slicing - each thread gets a few milliseconds to execute before the OS schedules another thread to run on that CPU. Since the OS switches back and forth between the threads quickly, it appears as if one CPU is doing more than one thing at once, but in reality, a core is still running only one hardware thread, which switches between many software threads.

Modern JVMs map java threads directly to the native threads provided by the OS, so there is no inherent overhead introduced by java threads vs native threads. As to hardware threads, the OS tries to map threads to cores, if there are sufficient cores. So, if you have a java program that starts 4 threads, and have 4 or more cores, there's a good chance your 4 threads will run truly in parallel on 4 separate cores, if the cores are idle.