[讨论] [HotSpot VM]《Java Performance》中关于线程描述的疑问
liuxiaori
2014-11-22
《Java Performance》中关于线程的有这样一段描述:
Java Performance 写道 When a java.lang.Thread is started the HotSpot VM creates the associated JavaThread and OSThread objects, and ultimately the native thread.
这里的native thread指的是什么?书中描述与线程相关的对象有java.lang.Thread, JavaThread, OSThread。难道指的是实际的操作系统的线程?而OSThread只是操作系统线程的一个描述。求指点。 |
|
RednaxelaFX
2014-11-23
这里的native thread是指操作系统支持的线程。在Windows上是_beginthreadex()(跟CreateThread()基本一样)创建的线程;在Posix系统上是pthread_create()创建的线程。
在Oracle JDK / OpenJDK上,一个Java线程背后有许多数据结构: Java层面 | HotSpot VM层面 | 操作系统层面 java.lang.Thread | JavaThread -> OSThread | native thread 在Java层面, http://hg.openjdk.java.net/jdk7u/jdk7u/jdk/file/cba625be1713/src/share/classes/java/lang/Thread.java#l151 package java.lang; public class Thread { // ... private long eetop; // 实为指向JavaThread的指针 // ... } 进到HotSpot VM层面,Thread/JavaThread用于记录(相对)平台无关的信息,而平台相关的信息则封装到OSThread里。 http://hg.openjdk.java.net/jdk7u/jdk7u/hotspot/file/9d2b485d2a58/src/share/vm/runtime/thread.hpp#l518 class Thread: public ThreadShadow { // ... // 指向OSThread的指针 OSThread* _osthread; // Platform-specific thread information // ... }; class JavaThread: public Thread { // ... }; http://hg.openjdk.java.net/jdk7u/jdk7u/hotspot/file/9d2b485d2a58/src/share/vm/runtime/osThread.hpp#l34 class OSThread: public CHeapObj<mtThread> { // ... // Platform dependent stuff #ifdef TARGET_OS_FAMILY_linux # include "osThread_linux.hpp" #endif // ... }; 然后平台相关的部分各自不同,以Linux为例的话是: http://hg.openjdk.java.net/jdk7u/jdk7u/hotspot/file/9d2b485d2a58/src/os/linux/vm/osThread_linux.hpp#l42 // _pthread_id is the pthread id, which is used by library calls // (e.g. pthread_kill). pthread_t _pthread_id; 从上面抽取的代码可以看出这几个数据结构的关系是(伪代码): java.lang.Thread thread; JavaThread* jthread = thread->_eetop; OSThread* osthread = jthread->_osthread; pthread_t pthread_id = osthread->_pthread_id; 这样可以理解清楚了? |
|
liuxiaori
2014-11-23
RednaxelaFX 写道 这里的native thread是指操作系统支持的线程。在Windows上是_beginthreadex()(跟CreateThread()基本一样)创建的线程;在Posix系统上是pthread_create()创建的线程。
在Oracle JDK / OpenJDK上,一个Java线程背后有许多数据结构: Java层面 | HotSpot VM层面 | 操作系统层面 java.lang.Thread | JavaThread -> OSThread | native thread 在Java层面, http://hg.openjdk.java.net/jdk7u/jdk7u/jdk/file/cba625be1713/src/share/classes/java/lang/Thread.java#l151 package java.lang; public class Thread { // ... private long eetop; // 实为指向JavaThread的指针 // ... } 进到HotSpot VM层面,Thread/JavaThread用于记录(相对)平台无关的信息,而平台相关的信息则封装到OSThread里。 http://hg.openjdk.java.net/jdk7u/jdk7u/hotspot/file/9d2b485d2a58/src/share/vm/runtime/thread.hpp#l518 class Thread: public ThreadShadow { // ... // 指向OSThread的指针 OSThread* _osthread; // Platform-specific thread information // ... }; class JavaThread: public Thread { // ... }; http://hg.openjdk.java.net/jdk7u/jdk7u/hotspot/file/9d2b485d2a58/src/share/vm/runtime/osThread.hpp#l34 class OSThread: public CHeapObj<mtThread> { // ... // Platform dependent stuff #ifdef TARGET_OS_FAMILY_linux # include "osThread_linux.hpp" #endif // ... }; 然后平台相关的部分各自不同,以Linux为例的话是: http://hg.openjdk.java.net/jdk7u/jdk7u/hotspot/file/9d2b485d2a58/src/os/linux/vm/osThread_linux.hpp#l42 // _pthread_id is the pthread id, which is used by library calls // (e.g. pthread_kill). pthread_t _pthread_id; 从上面抽取的代码可以看出这几个数据结构的关系是(伪代码): java.lang.Thread thread; JavaThread* jthread = thread->_eetop; OSThread* osthread = jthread->_osthread; pthread_t pthread_id = osthread->_pthread_id; 这样可以理解清楚了? R大简直神一样的存在,太详细了。有理有据,榜样。感谢 |