今天看啥  ›  专栏  ›  AlanKim

Thread相关学习之五 - Rules & Examples

AlanKim  · 简书  ·  · 2019-02-19 00:16
JVMTIThreadState的设置规则:
规则一

​ There can be no more than one answer to a question, although there can be no answer (because the answer is unknown, does not apply, or none of the answers is correct).

An answer is set only when the enclosing answers match. That is, no more than one of

  • JVMTI_THREAD_STATE_RUNNABLE
  • JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER
  • JVMTI_THREAD_STATE_WAITING

​ can be set (a J2SE TM compliant implementation will always set one of these if JVMTI_THREAD_STATE_ALIVE is set).

上述三个状态是互斥的,也就是一个线程只能同时设置三选一。

And if any of these are set, the enclosing answer JVMTI_THREAD_STATE_ALIVE is set.

如果其中一个设置了,那么JVMTI_THREAD_STATE_ALIVE这个状态也同时做了设置。(比如在JavaThread中状态为Runnable,那么实际底层的状态为 JVMTI_THREAD_STATE_ALIVE + JVMTI_THREAD_STATE_RUNNABLE)

规则2

No more than one of

  • JVMTI_THREAD_STATE_WAITING_INDEFINITELY
  • JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT

can be set (a J2SE TM compliant implementation will always set one of these if JVMTI_THREAD_STATE_WAITING is set).

And if either is set, the enclosing answers JVMTI_THREAD_STATE_ALIVE and JVMTI_THREAD_STATE_WAITING are set.

规则3

No more than one of

  • JVMTI_THREAD_STATE_IN_OBJECT_WAIT
  • JVMTI_THREAD_STATE_PARKED
  • JVMTI_THREAD_STATE_SLEEPING

can be set. And if any of these is set, the enclosing answers JVMTI_THREAD_STATE_ALIVE

and JVMTI_THREAD_STATE_WAITING are set.

规则4

Also, if JVMTI_THREAD_STATE_SLEEPING is set, then JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT is set.

If a state A is implemented using the mechanism of state B, then it is state A which is returned by this function.

For example, if

Thread.sleep(long)

is implemented using

Object.wait(long)

then it is still

JVMTI_THREAD_STATE_SLEEPING

which is returned.

规则5

More than one of

  • JVMTI_THREAD_STATE_SUSPENDED
  • JVMTI_THREAD_STATE_INTERRUPTED
  • JVMTI_THREAD_STATE_IN_NATIVE

can be set, but if any is set, JVMTI_THREAD_STATE_ALIVE is set.

规则6

And finally, JVMTI_THREAD_STATE_TERMINATED cannot be set unless JVMTI_THREAD_STATE_ALIVE is not set.

The thread state representation is designed for extension in future versions of the specification; thread state values should be used accordingly, that is they should not be used as ordinals. Most queries can be made by testing a single bit, if use in a switch statement is desired, the state bits should be masked with the interesting bits. All bits not defined above are reserved for future use. A VM, compliant to the current specification, must set reserved bits to zero. An agent should ignore reserved bits -- they should not be assumed to be zero and thus should not be included in comparisons.

Examples

​ Note that the values below exclude reserved and vendor bits.

​ The state of a thread blocked at a synchronized-statement would be:

JVMTI_THREAD_STATE_ALIVE + JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER

​ The state of a thread which hasn't started yet would be:

 0

​ The state of a thread at a

Object.wait(3000)

would be:

JVMTI_THREAD_STATE_ALIVE + 
JVMTI_THREAD_STATE_WAITING +
JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT +
JVMTI_THREAD_STATE_MONITOR_WAITING

​ The state of a thread suspended while runnable would be:

JVMTI_THREAD_STATE_ALIVE + 
JVMTI_THREAD_STATE_RUNNABLE + 
JVMTI_THREAD_STATE_SUSPENDED




原文地址:访问原文地址
快照地址: 访问文章快照