How Java multi-threaded program is able to use multiple CPU cores?

14,057

Solution 1

The Linux kernel supports threads as first-class citizens. In fact to the kernel a thread isn't much different to a process, except that it shares a address space with another thread/process.

Some old versions of ps even showed a separate process for each thread by default and newer versions can enable this behavior using the -m flag.

Solution 2

The JVM is a single process with many threads. Each thread can be scheduled on a different CPU core. A single process can have many threads.

When Java software running inside the JVM asks for another thread the JVM starts another thread.

That is how the JVM manages to use multiple cores.

Solution 3

If you use the concurrency library and split up your work as much as you can, the JVM should handle the rest.

Take a look at this http://embarcaderos.net/2011/01/23/parallel-processing-and-multi-core-utilization-with-java/

Solution 4

I would start by reading the Concurrency Tutorial.

In particular, it explains the differences (and relationship) between processes and threads.

On the architectures that I'm familiar with, the threads (including JVM-created threads) are managed by the OS. The JVM simply uses the threading facilities provided by the operating system.

Share:
14,057

Related videos on Youtube

Tomasz Błachowicz
Author by

Tomasz Błachowicz

Updated on April 12, 2020

Comments

  • Tomasz Błachowicz
    Tomasz Błachowicz about 4 years

    Could someone please provide explanation how Java multi-threaded program (e.g. Tomcat servlet container) is able to use all cores of CPU when JVM is only single process on linux? Is there any good in-depth article that describes the subject in details?

    EDIT #1: I'm not looking for advice how to implement multi-threaded program in Java. I'm looking for explanation of how JVM internally manages to use multiple cores on linux/windows while still being single process on the OS.

    EDIT #2: The best explanation I managed to find is that Hotspot (Sun/Oracle JVM) implements threads as native threads on Linux using NPTL. So more less each thread in Java is lightweight process (native thread) on Linux. It is clearly visible using ps -eLf command that print outs not only process id (PPID) but also native thread id (LWP).

    More details can be also found here:

    EDIT #3: Wikipedia has short but nice entry on NPTL with some further references http://en.wikipedia.org/wiki/Native_POSIX_Thread_Library

    • Brett
      Brett about 13 years
      I think you may be confused by "green threads" which was an old hack to support threads without thread support from the OS. Whilst there might be similar hacks today, most OSs support threads well enough.
  • Thomas
    Thomas about 13 years
    I'd say the OP knows that. The question rather was why the JVM shows up as a single process but still uses multiple cores, which Joachim already explained.
  • Tomasz Błachowicz
    Tomasz Błachowicz about 13 years
    My apologies of being not clear, but I'm not looking for advice how to implement multi-threaded program in Java. I'm looking for explanation of how JVM internally manages to use multiple cores on linux/windows while still being single process on the OS.
  • Tomasz Błachowicz
    Tomasz Błachowicz about 13 years
    I think it is important to mention that Hotspot (Sun/Oracle JVM) implements threads as native threads on Linux (lightweight processes)
  • skeggse
    skeggse about 9 years
    While the question was specifically asking about the JVM, it's also worth noting that not all java virtual machines support native threads (and in some cases native threads aren't even available) - this is more common on embedded systems.

Related