Java多线程程序如何使用多个CPU内核? [英] How Java multi-threaded program is able to use multiple CPU cores?

查看:811
本文介绍了Java多线程程序如何使用多个CPU内核?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

编辑#1 :我不是在寻求建议如何在Java中实现多线程程序。我正在寻找JVM内部如何在Linux / Windows上使用多个内核同时仍在操作系统上进行单一处理的解释。

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 :我设法找到的最佳解释是Hotspot(Sun / Oracle JVM)使用NPTL在Linux上将线程实现为本机线程。因此,Java中的每个线程都是Linux上的轻量级进程(本机线程)。使用 ps -eLf 命令可以清楚地看到打印输出不仅是进程ID( PPID )而且还有本机线程ID ( LWP )。

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?

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

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

推荐答案

Linux内核支持线程作为一等公民。事实上,对于内核,一个线程与一个进程没有太大的不同,除了它与另一个线程/进程共享一个地址空间。

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.

<$>的一些旧版本c $ c> ps 默认情况下,每个线程都显示一个单独的进程,较新的版本可以使用 -m 标志启用此行为。

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.

这篇关于Java多线程程序如何使用多个CPU内核?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆