看了很多关于线程状态介绍的文章,但是感觉还是JDK源码的注释解释的最清楚。下面是JDK1.8 Thread类关于线程状态的定义:
class Thread implements Runnable { /* Make sure registerNatives is the first thing <clinit> does. */ ... ... public enum State { /** * Thread state for a thread which has not yet started. */ NEW, /** * 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. */ RUNNABLE, /** * 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 * {@link Object#wait() Object.wait}. */ BLOCKED, /** * Thread state for a waiting thread. * A thread is in the waiting state due to calling one of the * following methods: * <ul> * <li>{@link Object#wait() Object.wait} with no timeout</li> * <li>{@link #join() Thread.join} with no timeout</li> * <li>{@link LockSupport#park() LockSupport.park}</li> * </ul> * * <p>A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called <tt>Object.wait()</tt> * on an object is waiting for another thread to call * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on * that object. A thread that has called <tt>Thread.join()</tt> * is waiting for a specified thread to terminate. */ 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: * <ul> * <li>{@link #sleep Thread.sleep}</li> * <li>{@link Object#wait(long) Object.wait} with timeout</li> * <li>{@link #join(long) Thread.join} with timeout</li> * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li> * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li> * </ul> */ TIMED_WAITING, /** * Thread state for a terminated thread. * The thread has completed execution. */ TERMINATED; }理解线程状态对分析thread dump很有帮助。这里谈一下自己的一点理解:
1. 关于线程的BLOCKED状态。当在thread dump中看到一个线程是BLOCKED状态时,就如Thread类中注释所说,一定是该线程在等待一个monitor lock,去进入(或重新进入)同步方法或同步代码块。
除了这种情况,有时我们把另外一种情况也说成线程阻塞了,比如一个线程尝试以同步阻塞的方式从socket读取数据,而socket的数据又没有准备好,我们这时会说线程被阻塞了。但是这种阻塞,根我们上边说的BLOCKED不是一回事,从thread dump看,这种情况下, 线程状态还是RUNNABLE的,如下所示:
"main" #1 prio=5 os_prio=0 tid=0x0000000002bd2800 nid=0x1bc0 runnable [0x0000000002a6f000] java.lang.Thread.State: RUNNABLE at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.SocketInputStream.read(SocketInputStream.java:171) at java.net.SocketInputStream.read(SocketInputStream.java:141) at java.net.SocketInputStream.read(SocketInputStream.java:127) at com.edu.TraditionalSocketDemo.main(TraditionalSocketDemo.java:26)2. WAITING和TIMED_WAITING状态与BLOCKED状态的区别。对于初学者来说,有时也会不小心把 WAITING和TIMED_WAITING状态的线程说成阻塞了,但是实际上它是等待状态,根BLOCKED状态是完全不一样的。不管是汉语的等待和阻塞,还是英语的block和wait,感觉意思都有些接近,或者说有些因果关系,字面意思容易混淆。如果非要说阻塞和等待字面意思有什么区别,也许阻塞是被动的,而等待是主动的?其实从程序的角度看,好像也是这样,BLOCKED是被动的等待,WAITING是主动的等待。
其实最重要是理解什么情况下会导致各自的状态,而状态的名称反而不是很重要。比如TIMED_WAITING, 代码注释里边就说的非常清楚,调用Thread.sleep(),Object.wati(),Thread.join(),LockSuppport.park()这些方法,并且方法参数中有时间参数,那就会导致这些处于这个状态。
同时要理解什么情况下需要调用这些函数,什么时候用到同步代码块,线程间如何同步信息等等,这些才是最重要的。这些理解之后,再回头看线程状态的区别,就会觉得很清晰了。
