往期文章:
继上一篇结尾讲的,这一篇文章主要是讲线程的生命周期以及状态流转。主要内容有:
先来谈一谈Java 中线程的状态。在 java.lang.Thread.State
类是 Thread
的内部枚举类,在里面定义了Java 线程的 六个状态 ,-注释信息也非常的详细。
public enum State { /** * Thread state for a thread which has not yet started. * 初始态,代表线程刚创建出来,但是还没有 start 的状态 */ 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. * * 运行态,代表线程正在运行或者等待操作系统资源,如CPU资源 */ 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}. * * 阻塞态,代表线程正在等待一个监视器锁(即我们常说的synchronized) * 或者是在调用了Object.wait之后被notify()重新进入synchronized代码块 */ 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. * * 等待态,调用以下方法会进入等待状态: * 1. 调用不会超时的Object.wait()方法 * 2. 调用不会超时的Thread.join()方法 * 3. 调用不会超时的LockSupport.park()方法 */ 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> * * 超时等待态,在调用了以下方法后会进入超时等待状态 * 1. Thread.sleep()方法后 * 2. Object.wait(timeout)方法 * 3. Thread.join(timeout)方法 * 4. LockSupport.parkNanos(nanos)方法 * 5. LockSupport.parkUntil(deadline)方法 */ TIMED_WAITING, /** * Thread state for a terminated thread. * The thread has completed execution. * * 终止态,代表线程已经执行完毕 */ TERMINATED; }
关于上面JDK源码中对于 BLOCKED
状态的注释,这里有一点需要补充的,就是如果是线程调用了 Object.wait(timeout)
方法进入 TIMED_WAITING
状态之后,如果是因为超过指定时间,脱离 TIMED_WAITING
状态,如果接下去线程是要重新进入 synchronize
代码块的话,也是会先进入等待队列,变成 BLOCKED
状态,然后请求监视器锁资源。
再来看,操作系统层面,线程存在五类状态,状态的流转关系可以参考下面的这张图。
可以看到,Java 中所说的线程状态和操作系统层面的线程状态是不太一样的。
RUNNABLE
其实包含了OS中的 RUNNING
和 READY
WATING
、 TIMED_WATING
、 BLOCKED
其实是对OS中 WAITING
状态的一个更细致的划分 在 THread.State
源码中也写了这么一句话:
These states are virtual machine states which do not reflect any operating system thread states.
对于这两个层面对比,你需要知道的是, Java的线程状态是服务于监控的 。从这个角度来考虑的话,把底层OS中的 RUNNING
和 READY
状态映射上来也没多大意义,因此,统一成为 RUNNABLE
状态是不错的选择,而对WATING状态更细致的划分,也是出于这么一个考虑。
图很详细,结合前面的内容一起食用,这里就不多说啥了。
这里演示一下,如何在IDEA 上面来验证上述的状态流转。有疑问或者有兴趣的读者可以按照同样的方法来验证。
我这里想要用代码验证下面的情况,
就是如果是线程1调用了 Object.wait(timeout)
方法进入 TIMED_WAITING
状态之后,如果是因为超过指定时间,脱离 TIMED_WAITING
状态,如果接下去线程是要重新进入 synchronize
代码块的话,也是会先进入等待队列,变成 BLOCKED
状态,然后请求监视器锁资源。
public class ThreadLifeTempTest { public static void main(String[] args) { Object object = new Object(); new Thread(()->{ synchronized (object) { try { System.out.println("thread1 waiting"); // 等待10s,进入Timed_Waiting // 10s 后会进入Blocked,获取object的监视器锁 object.wait(10000); System.out.println("thread1 after waiting"); } catch (InterruptedException e) { e.printStackTrace(); } } }, "Thread1").start(); new Thread(()->{ synchronized (object) { try { // sleep也不会释放锁,所以thread1 不会获取到锁 Thread.sleep(10000000); } catch (InterruptedException e) { e.printStackTrace(); } } }, "Thread2").start(); } }
使用 IDEA的RUN模式运行代码 ,然后点击左边的一个摄像头按钮(dump thread),查看各线程的状态。
在Thread 1 等待 10s中时,dump的结果:Thread 1和Thread 2都处于 TIMED_WAITING
状态,
"Thread2" #13 prio=5 os_prio=0 tid=0x0000000020196800 nid=0x65b8 waiting on condition [0x0000000020afe000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at main.java.concurrent.thread.ThreadLifeTempTest.lambda$main$1(ThreadLifeTempTest.java:33) - locked <0x000000076b71c748> (a java.lang.Object) at main.java.concurrent.thread.ThreadLifeTempTest$$Lambda$2/1096979270.run(Unknown Source) at java.lang.Thread.run(Thread.java:748) "Thread1" #12 prio=5 os_prio=0 tid=0x0000000020190800 nid=0x25fc in Object.wait() [0x00000000209ff000] java.lang.Thread.State: TIMED_WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x000000076b71c748> (a java.lang.Object) at main.java.concurrent.thread.ThreadLifeTempTest.lambda$main$0(ThreadLifeTempTest.java:21) - locked <0x000000076b71c748> (a java.lang.Object) at main.java.concurrent.thread.ThreadLifeTempTest$$Lambda$1/1324119927.run(Unknown Source) at java.lang.Thread.run(Thread.java:748)
在Thread 1 等待 10s之后,Thread 1重新进入 synchronize
代码块,进入等待队列,变成 BLOCKED
状态
"Thread2" #13 prio=5 os_prio=0 tid=0x0000000020196800 nid=0x65b8 waiting on condition [0x0000000020afe000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at main.java.concurrent.thread.ThreadLifeTempTest.lambda$main$1(ThreadLifeTempTest.java:33) - locked <0x000000076b71c748> (a java.lang.Object) at main.java.concurrent.thread.ThreadLifeTempTest$$Lambda$2/1096979270.run(Unknown Source) at java.lang.Thread.run(Thread.java:748) "Thread1" #12 prio=5 os_prio=0 tid=0x0000000020190800 nid=0x25fc waiting for monitor entry [0x00000000209ff000] java.lang.Thread.State: BLOCKED (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x000000076b71c748> (a java.lang.Object) at main.java.concurrent.thread.ThreadLifeTempTest.lambda$main$0(ThreadLifeTempTest.java:21) - locked <0x000000076b71c748> (a java.lang.Object) at main.java.concurrent.thread.ThreadLifeTempTest$$Lambda$1/1324119927.run(Unknown Source) at java.lang.Thread.run(Thread.java:748)