Java 多线程程序如何能够使用多个 CPU 内核?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5622373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:57:38  来源:igfitidea点击:

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

javamultithreadingjvm

提问by Tomasz B?achowicz

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?

有人可以解释当JVM只是Linux上的单个进程时Java多线程程序(例如Tomcat servlet容器)如何能够使用CPU的所有核心?有没有什么好的深入的文章详细描述这个主题?

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.

编辑 #1:我不是在寻找如何在 Java 中实现多线程程序的建议。我正在寻找有关 JVM 如何在内部管理在 linux/windows 上使用多个内核,同时仍然是操作系统上的单个进程的解释。

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 -eLfcommand that print outs not only process id (PPID) but also native thread id (LWP).

编辑 #2:我设法找到的最佳解释是 Hotspot(Sun/Oracle JVM)使用 NPTL 在 Linux 上将线程实现为本机线程。因此,Java 中的每个线程在 Linux 上都是轻量级进程(本机线程)。使用ps -eLf不仅打印出进程 id ( PPID) 还打印出本机线程 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

编辑 #3:维基百科在 NPTL 上有简短但不错的条目,还有一些进一步的参考http://en.wikipedia.org/wiki/Native_POSIX_Thread_Library

采纳答案by Joachim Sauer

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.

Linux 内核支持线程作为一等公民。事实上,对于内核来说,线程与进程没有太大区别,只是它与另一个线程/进程共享一个地址空间。

Some old versions of pseven showed a separate process for each thread by default and newer versions can enable this behavior using the -mflag.

一些旧版本ps甚至默认为每个线程显示一个单独的进程,新版本可以使用该-m标志启用此行为。

回答by Zan Lynx

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.

JVM 是具有多个线程的单个进程。每个线程都可以在不同的 CPU 内核上进行调度。一个进程可以有多个线程。

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

当在 JVM 中运行的 Java 软件请求另一个线程时,JVM 会启动另一个线程。

That is how the JVM manages to use multiple cores.

这就是 JVM 设法使用多个内核的方式。

回答by bjornhol

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

如果您使用并发库并尽可能多地拆分您的工作,那么 JVM 应该处理其余的工作。

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

看看这个http://embarcaderos.net/2011/01/23/parallel-processing-and-multi-core-utilization-with-java/

回答by NPE

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.

在我熟悉的架构上,线程(包括 JVM 创建的线程)由操作系统管理。JVM 只是使用操作系统提供的线程工具。