Thread相关学习之四 - ThreadStatus

javaClasses.hpp中对ThreadStatus的定义

不过在jvm中,javaClasses.hpp中有一个枚举定义,如下:

// Java Thread Status for JVMTI and M&M use.
// This thread status info is saved in threadStatus field of
// java.lang.Thread java class.  
enum ThreadStatus {
    NEW                      = 0,
    RUNNABLE                 = JVMTI_THREAD_STATE_ALIVE +          // runnable / running
                               JVMTI_THREAD_STATE_RUNNABLE,
    SLEEPING                 = JVMTI_THREAD_STATE_ALIVE +          // Thread.sleep()
                               JVMTI_THREAD_STATE_WAITING +
                               JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT +
                               JVMTI_THREAD_STATE_SLEEPING,
    IN_OBJECT_WAIT           = JVMTI_THREAD_STATE_ALIVE +          // Object.wait()
                               JVMTI_THREAD_STATE_WAITING +
                               JVMTI_THREAD_STATE_WAITING_INDEFINITELY +
                               JVMTI_THREAD_STATE_IN_OBJECT_WAIT,
    IN_OBJECT_WAIT_TIMED     = JVMTI_THREAD_STATE_ALIVE +          // Object.wait(long)
                               JVMTI_THREAD_STATE_WAITING +
                               JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT +
                               JVMTI_THREAD_STATE_IN_OBJECT_WAIT,
    PARKED                   = JVMTI_THREAD_STATE_ALIVE +          // LockSupport.park()
                               JVMTI_THREAD_STATE_WAITING +
                               JVMTI_THREAD_STATE_WAITING_INDEFINITELY +
                               JVMTI_THREAD_STATE_PARKED,
    PARKED_TIMED             = JVMTI_THREAD_STATE_ALIVE +          // LockSupport.park(long)
                               JVMTI_THREAD_STATE_WAITING +
                               JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT +
                               JVMTI_THREAD_STATE_PARKED,
    BLOCKED_ON_MONITOR_ENTER = JVMTI_THREAD_STATE_ALIVE +          // (re-)entering a synchronization block
                               JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER,
    TERMINATED               = JVMTI_THREAD_STATE_TERMINATED
  };

可以看到,在jvm中定义了更多的线程状态。注意看其中以JVMTI_开头的数据,就是上面在jvmti.xml中定义的常量。可以看到,比JavaThreadState多了3种状态:SLEEPING、PARKED、TIME_PARKED。为什么呢?

JVM中线程状态的分类

依然看jvmti.xml(用firefox打开),可以看到如下内容:

Get Thread State

jvmtiError
GetThreadState(jvmtiEnv* env,
            jthread thread,
            jint* thread_state_ptr)

​ Get the state of a thread. The state of the thread is represented by the answers to the hierarchical set of questions below:

  • Alive?
    • Not alive.
      • Why not alive?
        • New.
        • Terminated ([JVMTI_THREAD_STATE_TERMINATED](file:///home/alanjin/gitspace/jdk10/hotspot/src/share/vm/prims/jvmti.xml#JVMTI_THREAD_STATE_TERMINATED))
    • Alive (JVMTI_THREAD_STATE_ALIVE)
      • Suspended?
        • Suspended ([JVMTI_THREAD_STATE_SUSPENDED](file:///home/alanjin/gitspace/jdk10/hotspot/src/share/vm/prims/jvmti.xml#JVMTI_THREAD_STATE_SUSPENDED))
        • Not suspended
      • Interrupted?
        • Interrupted ([JVMTI_THREAD_STATE_INTERRUPTED](file:///home/alanjin/gitspace/jdk10/hotspot/src/share/vm/prims/jvmti.xml#JVMTI_THREAD_STATE_INTERRUPTED))
        • Not interrupted.
      • In native?
        • In native code ([JVMTI_THREAD_STATE_IN_NATIVE](file:///home/alanjin/gitspace/jdk10/hotspot/src/share/vm/prims/jvmti.xml#JVMTI_THREAD_STATE_IN_NATIVE))
        • In Java programming language code
      • What alive state?
        • Runnable ([JVMTI_THREAD_STATE_RUNNABLE](file:///home/alanjin/gitspace/jdk10/hotspot/src/share/vm/prims/jvmti.xml#JVMTI_THREAD_STATE_RUNNABLE))
        • Blocked ([JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER](file:///home/alanjin/gitspace/jdk10/hotspot/src/share/vm/prims/jvmti.xml#JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER))
        • Waiting (JVMTI_THREAD_STATE_WAITING)
          • Timed wait?
            • Indefinite ([JVMTI_THREAD_STATE_WAITING_INDEFINITELY](file:///home/alanjin/gitspace/jdk10/hotspot/src/share/vm/prims/jvmti.xml#JVMTI_THREAD_STATE_WAITING_INDEFINITELY))
            • Timed ([JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT](file:///home/alanjin/gitspace/jdk10/hotspot/src/share/vm/prims/jvmti.xml#JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT))
          • Why waiting?
            • Object.wait ([JVMTI_THREAD_STATE_IN_OBJECT_WAIT](file:///home/alanjin/gitspace/jdk10/hotspot/src/share/vm/prims/jvmti.xml#JVMTI_THREAD_STATE_IN_OBJECT_WAIT))
            • LockSupport.park ([JVMTI_THREAD_STATE_PARKED](file:///home/alanjin/gitspace/jdk10/hotspot/src/share/vm/prims/jvmti.xml#JVMTI_THREAD_STATE_PARKED))
            • Sleeping ([JVMTI_THREAD_STATE_SLEEPING](file:///home/alanjin/gitspace/jdk10/hotspot/src/share/vm/prims/jvmti.xml#JVMTI_THREAD_STATE_SLEEPING))

可以看到,在JVM中,对线程的状态会有各种分支分类。而把Sleeping\Parked其实是放在了WAITING范畴内.也就是说,Java Thread只是上层的一层包装,而作为底层的JVMThread,实现上层包装,则需要多个底层状态的组合。

你可能感兴趣的:(Thread相关学习之四 - ThreadStatus)