java线程状态及线程状态机切换流程,如何查看JVM线程状态
java的线程状态
线程的6大状态:NEW-新建、RUNNABLE-线程执行、BLOCKED-被阻塞、
WAITING-等待、TIMED_WAITING-计时等待、TERMINATED-终止。
我们可以从java源码java.lang.Thread.State中,可得知java线程的状态分类:
NEW-新建
A thread that has not yet started is in this state.
新创建的线程,即java.lang.Thread构造函数被调用后,还没调用
java.lang.Thread#start方法。
RUNNABLE-线程执行
Thread state for a runnable thread. A thread in the runnable state is executing in the Java virtual machine but it may be waiting for other resources from the operating system such as processor.
线程在JVM中已被执行,即调用java.lang.Thread#start方法后。此时线程占用CPU资源等资源执行中或等待CPU资源等,即将被调度系统调度执行的状态。
BLOCKED-被阻塞
Thread state for a thread blocked waiting for a monitor lock. A thread in the blocked state is waiting for a monitor lock to enter a synchronized block/method or reenter a synchronized block/method after calling Object.wait.
获取监视器资源即锁时处于等待的状态。
WAITING-等待
Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods:
由于调用以下方法,使得线程处于等待状态。
1、Object.wait with no timeout
2、Thread.join with no timeout
3、LockSupport.park
A thread in the waiting state is waiting for another thread to perform a particular action. For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on that object. A thread that has called Thread.join() is waiting for a specified thread to terminate.
TIMED_WAITING-计时等待
Thread state for a waiting thread with a specified waiting time. A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time:
线程等待状态,并且设置了预期的等待时间值。由于调用以下方法,会导致线程处于
1、Thread.sleep
2、Object.wait with timeout
3、Thread.join with timeout
4、LockSupport.parkNanos
5、LockSupport.parkUntil
TERMINATED-终止
Thread state for a terminated thread. The thread has completed.
线程结束状态。线程执行完毕。
线程状态机
来一张图总结一下
来源:https://www.uml-diagrams.org/java-thread-uml-state-machine-diagram-example.html
如何查询线程状态
1、命令jstack
jstack -l <pid> > <file-path>
示例:
2022-04-30 23:46:54
Full thread dump OpenJDK 64-Bit Server VM (11.0.13+7-b1751.25 mixed mode):
Threads class SMR info:
_java_thread_list=0x0000600004fc1880, length=46, elements={
0x00007fc91b81a800, 0x00007fc91c00a800, 0x00007fc919021800, 0x00007fc919016800,
0x00007fc919017800, 0x00007fc91901a000, 0x00007fc91901b000, 0x00007fc91a01c800,
0x00007fc91a88f800, 0x00007fc920010000, 0x00007fc91aa3a000, 0x00007fc91c874000,
0x00007fc91a0a0000, 0x00007fc91a0af800, 0x00007fc91a0ad000, 0x00007fc91a11f000,
0x00007fc919992000, 0x00007fc9200af800, 0x00007fc919220800, 0x00007fc91b809000,
0x00007fc91f301000, 0x00007fc91c622000, 0x00007fc919cf8800, 0x00007fc919c0d000,
0x00007fc91a1ba800, 0x00007fc91a51f800, 0x00007fc91a46a800, 0x00007fc919b81800,
0x00007fc926115000, 0x00007fc9260cb000, 0x00007fc926817000, 0x00007fc91963f000,
0x00007fc926008800, 0x00007fc926009000, 0x00007fc91a276800, 0x00007fc926378800,
0x00007fc91a277800, 0x00007fc91a27e800, 0x00007fc91cc79800, 0x00007fc91ea5b000,
0x00007fc91c02b800, 0x00007fc920a38800, 0x00007fc91f308000, 0x00007fc91f3c9800,
0x00007fc90cdc7800, 0x00007fc91f132800
}
"Reference Handler" #2 daemon prio=10 os_prio=31 cpu=132.66ms elapsed=14508.18s tid=0x00007fc91b81a800 nid=0x3e03 waiting on condition [0x0000700008111000]
java.lang.Thread.State: RUNNABLE
at java.lang.ref.Reference.waitForReferencePendingList(java.base@11.0.13/Native Method)
at java.lang.ref.Reference.processPendingReferences(java.base@11.0.13/Reference.java:241)
at java.lang.ref.Reference$ReferenceHandler.run(java.base@11.0.13/Reference.java:213)
Locked ownable synchronizers:
- None
"C2 CompilerThread0" #6 daemon prio=9 os_prio=31 cpu=165892.13ms elapsed=14508.16s tid=0x00007fc919017800 nid=0xa503 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
No compile task
Locked ownable synchronizers:
- None
.......
"G1 Young RemSet Sampling" os_prio=31 cpu=1334.32ms elapsed=14508.23s tid=0x00007fc91a870800 nid=0x3603 runnable
"VM Periodic Task Thread" os_prio=31 cpu=1862.51ms elapsed=14507.99s tid=0x00007fc91b850800 nid=0x9e03 waiting on condition
JNI global refs: 316, weak refs: 683
2、在线工具fastthread
https://fastthread.io
示例:
3、在线工具https://spotify.github.io/threaddump-analyzer/
https://spotify.github.io/threaddump-analyzer/
示例:
小结
线程的6大状态:NEW-新建、RUNNABLE-线程执行、BLOCKED-被阻塞、
WAITING-等待、TIMED_WAITING-计时等待、TERMINATED-终止。
学会使用分析工具分析线程的状态,比如应用卡在的时候,是不是死锁了。分析线程的使用率,有利于优化线程数量。