How java threads are scheduled?

前端 未结 4 2086
后悔当初
后悔当初 2021-01-06 09:00

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 schedule

相关标签:
4条回答
  • 2021-01-06 09:10

    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/

    0 讨论(0)
  • 2021-01-06 09:20

    Read Distinguish Java threads and OS threads? As I said in the comment Java threads are ordinary OS threads just running JVM code

    0 讨论(0)
  • 2021-01-06 09:22

    "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

    0 讨论(0)
  • 2021-01-06 09:32

    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

    0 讨论(0)
提交回复
热议问题