想起来写一下Java线程状态,还是源起与最近的一次问题定位,当时碰到一个偶先超时的问题,使用jstack命令打印出堆栈信息之后,例如
"transport-vert.x-eventloop-thread-11" #37 prio=5 os_prio=0 tid=0x00007f628d0f8800 nid=0x2a32 runnable [0x00007f62631f8000]
java.lang.Thread.State: RUNNABLE
at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93)
at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
- locked <0x00000006f3cb4310> (a io.netty.channel.nio.SelectedSelectionKeySet)
- locked <0x00000006f3cb43d8> (a java.util.Collections$UnmodifiableSet)
- locked <0x00000006f3cb42c8> (a sun.nio.ch.EPollSelectorImpl)
at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
at io.netty.channel.nio.SelectedSelectionKeySetSelector.select(SelectedSelectionKeySetSelector.java:62)
at io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:755)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:410)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)
通过定位线程的状态,找到了错误的原因,也稍微费了些劲,所以想到把线程的一些状态再这里面总结一下
线程的6种状态
Java线程Thread在package java.lang;中可以找到,通过源码,我们可以看到其状态有如下6种
NEW
RUNNABLE
BLOCKED
WAITING
TIMED_WAITING
TERMINATED
下面分别解释一下各种状态
NEW
顾名思义,这个状态,只存在于线程刚创建,未start之前&#