Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

  • http://www.velocityreviews.com/forums/t499841-java-5-threads-in-linux.html
  • Distinguishing between Java threads and OS threads?

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

like image 704
Tomasz Błachowicz Avatar asked Apr 11 '11 13:04

Tomasz Błachowicz


People also ask

Does multithreading in Java use multiple cores?

Java will benefit from multiple cores, if the OS distribute threads over the available processors. JVM itself do not do anything special to get its threads scheduled evenly across multiple cores.

Does multi threading require multiple cores?

Even if you have only one core, you can still run multiple threads, and your OS will do its best to make sure that all of the running threads in all of the running processes get their fair share of CPU time.

How does Java handle multiple threads?

We create a class that extends the java. This class overrides the run() method available in the Thread class. A thread begins its life inside run() method. We create an object of our new class and call start() method to start the execution of a thread. Start() invokes the run() method on the Thread object.

Does Java support multi core?

Does Java have support for multicore processors/parallel processing? Yes. It also has been a platform for other programming languages where the implementation added a "true multithreading" or "real threading" selling point.


2 Answers

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.

like image 70
Joachim Sauer Avatar answered Nov 10 '22 11:11

Joachim Sauer


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.

like image 23
Zan Lynx Avatar answered Nov 10 '22 10:11

Zan Lynx