More than 80% of the java thread state diagrams are drawn wrong, take a look at this-Graphical Java Concurrency Part 2

This article serves as the second article to illustrate java concurrent programming. The access address of the previous article is as follows:

Graphic description

Before I started thinking about writing this article, I searched the Internet for a lot of graphs about thread state transitions, and I was surprised to find that more than 80% of the graphs are incomplete or wrong. So I had to read the source code again, and then drew the following picture. After understanding this picture, it will be very helpful for everyone to understand the method call and development process of java threads.

  • The synchronous blocking state is easier to understand, that is, the mutex, the code block used by the current thread, you can only use it when I run out.
  • The main difference between the delayed wait state and the wait state is
    • Delayed waiting state will not release any resources and monitors. I just occupy the pit and don't shit, and I won't use it for you. I just take a break, and when I'm done, mine is still mine.
    • Waiting for the blocking state will temporarily release related thread resources and monitors. My resources are temporarily loaned to you. You must return the resources to me when I notify you.

Thread.State source code comment

public enum State {
    /**
     *  新生状态:线程对象创建,但是还未start()
     */
    NEW,

    /**
     * 线程处于可运行状态,但是这个可运行状态并不代表线程一定在虚拟机中执行。
     * 需要等待从操作系统获取到资源(比如处理器时间片),才能真正的去运行
     */
    RUNNABLE,

    /**
     * 当前线程处于阻塞状态,正在等待另一个线程的monitor lock释放,才进入synchronized代码块或方法
     */
    BLOCKED,

    /**
     * 调用Object#wait() 、 Thread.join方法后当前线程处于等待状态,
     * 等待其他的线程执行特定的动作,才能从等待状态退出。
     * 比如:Object.wait()的线程需要等待其他线程调用Object.notify()、Object.notifyAll()才能退出
     * 比如:调用了Thread.join()的线程需要等待指定的线程执行完成才能退出等待状态。
     */
    WAITING,

    /**
     * 进入特定时间内的等待状态,等待一段指定的时间sleep(timed)、wait(timed)或者等待Thread.join(timed)的时间.
     * 到达指定时间点自动退出恢复到RUNNABLE状态
     */
    TIMED_WAITING,

    /**
      * 线程结束状态
     */
    TERMINATED;
}

Welcome to follow my blog, there are many boutique collections

  • This article is reproduced indicate the source (en must not turn only the text): letters Gebo off .

If you think it is helpful to you, please like and share it for me! Your support is my inexhaustible creative motivation! . In addition, the author has output the following high-quality content recently, and I look forward to your attention.

Guess you like

Origin blog.csdn.net/hanxiaotongtong/article/details/108507627