Thread相关学习之二 - JavaThread&JV
2019-02-17 本文已影响6人
AlanKim
线程生命周期
JavaThread生命周期
线程生命周期就在上述的6个状态中流转,如下图:

JVMTIThreadState定义
不过在jvmti.xml中,看到了jvmti(JVM Tool Interface)定义的相关ThreadState,看下:
<constants id="jvmtiThreadState" label="Thread State Flags" kind="bits">
<constant id="JVMTI_THREAD_STATE_ALIVE" num="0x0001">
Thread is alive. Zero if thread is new (not started) or terminated.
</constant>
<constant id="JVMTI_THREAD_STATE_TERMINATED" num="0x0002">
Thread has completed execution.
</constant>
<constant id="JVMTI_THREAD_STATE_RUNNABLE" num="0x0004">
Thread is runnable.
</constant>
<constant id="JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER" num="0x0400">
Thread is waiting to enter a synchronization block/method or,
after an <code>Object.wait()</code>, waiting to re-enter a
synchronization block/method.
</constant>
<constant id="JVMTI_THREAD_STATE_WAITING" num="0x0080">
Thread is waiting.
</constant>
<constant id="JVMTI_THREAD_STATE_WAITING_INDEFINITELY" num="0x0010">
Thread is waiting without a timeout.
For example, <code>Object.wait()</code>.
</constant>
<constant id="JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT" num="0x0020">
Thread is waiting with a maximum time to wait specified.
For example, <code>Object.wait(long)</code>.
</constant>
<constant id="JVMTI_THREAD_STATE_SLEEPING" num="0x0040">
Thread is sleeping -- <code>Thread.sleep(long)</code>.
</constant>
<constant id="JVMTI_THREAD_STATE_IN_OBJECT_WAIT" num="0x0100">
Thread is waiting on an object monitor -- <code>Object.wait</code>.
</constant>
<constant id="JVMTI_THREAD_STATE_PARKED" num="0x0200">
Thread is parked, for example: <code>LockSupport.park</code>,
<code>LockSupport.parkUtil</code> and <code>LockSupport.parkNanos</code>.
</constant>
<constant id="JVMTI_THREAD_STATE_SUSPENDED" num="0x100000">
Thread suspended.
<code>java.lang.Thread.suspend()</code>
or a <jvmti/> suspend function
(such as <functionlink id="SuspendThread"></functionlink>)
has been called on the thread. If this bit
is set, the other bits refer to the thread state before suspension.
</constant>
<constant id="JVMTI_THREAD_STATE_INTERRUPTED" num="0x200000">
Thread has been interrupted.
</constant>
<constant id="JVMTI_THREAD_STATE_IN_NATIVE" num="0x400000">
Thread is in native code--that is, a native method is running
which has not called back into the VM or Java programming
language code.
<p/>
This flag is not set when running VM compiled Java programming
language code nor is it set when running VM code or
VM support code. Native VM interface functions, such as JNI and
<jvmti/> functions, may be implemented as VM code.
</constant>
<constant id="JVMTI_THREAD_STATE_VENDOR_1" num="0x10000000">
Defined by VM vendor.
</constant>
<constant id="JVMTI_THREAD_STATE_VENDOR_2" num="0x20000000">
Defined by VM vendor.
</constant>
<constant id="JVMTI_THREAD_STATE_VENDOR_3" num="0x40000000">
Defined by VM vendor.
</constant>
</constants>
可以看到,这里使用十六进制表示一个具体的ThreadState值。整理一下:
Thread State Flags | ||
---|---|---|
Constant | Value | Description |
JVMTI_THREAD_STATE_ALIVE |
0x0001 | Thread is alive. Zero if thread is new (not started) or terminated. |
JVMTI_THREAD_STATE_TERMINATED |
0x0002 | Thread has completed execution. |
JVMTI_THREAD_STATE_RUNNABLE |
0x0004 | Thread is runnable. |
JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER |
0x0400 | Thread is waiting to enter a synchronization block/method or, after an Object.wait() , waiting to re-enter a synchronization block/method. |
JVMTI_THREAD_STATE_WAITING |
0x0080 | Thread is waiting. |
JVMTI_THREAD_STATE_WAITING_INDEFINITELY |
0x0010 | Thread is waiting without a timeout. For example, Object.wait() . |
JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT |
0x0020 | Thread is waiting with a maximum time to wait specified. For example, Object.wait(long) . |
JVMTI_THREAD_STATE_SLEEPING |
0x0040 | Thread is sleeping -- Thread.sleep(long) . |
JVMTI_THREAD_STATE_IN_OBJECT_WAIT |
0x0100 | Thread is waiting on an object monitor -- Object.wait . |
JVMTI_THREAD_STATE_PARKED |
0x0200 | Thread is parked, for example: LockSupport.park , LockSupport.parkUtil and LockSupport.parkNanos . |
JVMTI_THREAD_STATE_SUSPENDED |
0x100000 | Thread suspended. java.lang.Thread.suspend() or a JVM TI suspend function (such as [SuspendThread ](file:///home/alanjin/gitspace/jdk10/hotspot/src/share/vm/prims/jvmti.xml#SuspendThread)) has been called on the thread. If this bit is set, the other bits refer to the thread state before suspension. |
JVMTI_THREAD_STATE_INTERRUPTED |
0x200000 | Thread has been interrupted. |
JVMTI_THREAD_STATE_IN_NATIVE |
0x400000 | Thread is in native code--that is, a native method is running which has not called back into the VM or Java programming language code. This flag is not set when running VM compiled Java programming language code nor is it set when running VM code or VM support code. Native VM interface functions, such as JNI and JVM TI functions, may be implemented as VM code. |
JVMTI_THREAD_STATE_VENDOR_1 |
0x10000000 | Defined by VM vendor. |
JVMTI_THREAD_STATE_VENDOR_2 |
0x20000000 | Defined by VM vendor. |
JVMTI_THREAD_STATE_VENDOR_3 |
0x40000000 | Defined by VM vendor. |
注意其中的值都是16进制数据,而实际上是用位(bit)来组合计算的。