问题
I have recently started multi-threaded programming with Java in case of Linux threads , i know that the kernel schedules them(as they are the unit entities that are scheduled)but java programs are run on JVM which in my system (RHEL 6.1) is implemented as a program that is run as a user space instance .So, without the kernel being aware of the java threads, how come preemptive multitasking is done in JVM? it will be helpful if the whole mechanism of JVM and kernel interaction in doing this thing is given.
Please cite possible sources of info
回答1:
Threads in the java/JVM process maps to a native thread and you can see both the java thread id and the native thread id in a thread stack trace dump. Get the thread stack of all java threads using your favorite tool:
- command line signal like ctrl+break (windows) or ctrl+\ linux) in the console where the java program is running
- command line tool (kill -QUIT or jstack from the jdk)
- visual vm in the jdk and/or jmx etc
Example extract from the first line of such a thread dump: ... tid=0x0000002adaba9c00 nid=0x754c ...
tid = java thread id
nid = native id (the OS thread id)
Use the operating system's tools to find out more about the thread using the native id (it is in hex).
Inside the java code you have ThreadMXBean to retrieve more thread information programatically if you want http://docs.oracle.com/javase/6/docs/api/java/lang/management/ThreadMXBean.html
回答2:
Read Distinguish Java threads and OS threads? As I said in the comment Java threads are ordinary OS threads just running JVM code
回答3:
The jvm is just a normal process which starts with one thread and can spawn as much threads it likes afterwards. Scheduling is done on two levels - between processes and between threads inside processes. All this is done by the OS (via libs) - the jvm just hooks in. Google posix threads for more details - thats whats exposed (API) to the jvm.
This goes a bit into details: http://www.ibm.com/developerworks/java/library/j-rtj3/
回答4:
"but java programs are run on JVM which in my system (RHEL 6.1) is implemented as a program that is run as a user space instance.So, without the kernel being aware of the java threads ..."
This statement is incorrect for all modern JVM's that use native threads. I think thats been the default since Java 1.2. Native Thread implementation by a JVM means that each time a thread instantiates/runs a thread in Java code, the JVM asks the OS to create the thread. Since these are native threads, the kernel knows about them and treats them accordingly. Furthermore, Linux supports/implements POSIX threads, and as such on a Linux based systems you will get pthread behavior for your Java apps' threads
来源:https://stackoverflow.com/questions/9890900/how-java-threads-are-scheduled