Java的阻塞、等待及相关方法

来自Wikioe
跳到导航 跳到搜索


前言

刚刚正在梳理Java并发部分的知识点,(《Java核心技术卷1》与以前做的笔记),发现这部分出入比较大。所以单开一个词条,用于相关笔记的整理。【2020/10/19 20:50:10】

关于线程状态

线程(进程)的运行状态,最基本的三种:“就绪”、“运行”、“阻塞”。在此基础上,不同的系统进行了扩展(如:“新建”、“死亡”、“等待”、“挂起”)形成了五状态、六状态、甚至七状态模型。


但是,在 Java(JVM) 中,仅有六种状态:

    These states are virtual machine states which do not reflect any operating system thread states.

    这些状态是虚拟机状态,不反映任何操作系统线程状态。

(网上查到的相关内容,很多都按五种状态分析,可能没看源码,按惯性思维来了,或者1.5以前版本是那样?)

根据“Thread”源码可以发现,其线程公有六种状态:“New”、“Runnable”、“Blocked”、“Waiting”、“Timed_Waiting”、“Terminated”。

    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;
    }

其中:

  1. Runnable”可以理解为“就绪”+“运行”;
  2. Blocked”:用于标识线程等待一个监视器锁
    如:【?】
    1. 进入 synchronized 标记的代码块或方法;
    2. 调用“Object.wait”之后,重入 synchronized 标记的代码块或方法;
  3. Waiting”:用于标识线程等待另一个线程执行特定操作
    如:
    1. “Object.wait()”
    2. “Thread.join()”
    3. “LockSupport.park()”
  4. Timed_Waiting”:具有指定等待时间的“Waiting”状态
    如:(方法带有时间参数)
    1. “Object.wait(long timeout)”
    2. “Thread.join(long millis)”
    3. “Thread.sleep(long millis)”
    4. “LockSupport.parkNanos(long nanos)”
    5. “LockSupport.parkUntil(long deadline)”
  • (并非像网上所说,“阻塞”分为:“同步阻塞”、“等待阻塞”、“其他阻塞”)

状态转换

找到了一个靠谱的状态转换图:

Java线程状态转换.png

相关方法

只有在线程 running 的时候,才会获取 cpu 片段

需要区分的几个方法:

  1. Object.wait释放cpu,释放锁,进入线程等待池中等待被再次唤醒(notify 随机唤醒,notifyAll 全部唤醒,线程结束自动唤醒),即放入锁池中竞争同步锁。
    wait 应该与 notify、notifyAll 搭配,用作对象内部锁“Synchronized”的内部条件
  2. Thread.sleep:(静态方法)释放cpu,保持锁(见源码注释“The thread does not lose ownership of any monitors”)。
  3. Thread.yeild:(静态方法)使当前线程让出CPU,进入线程池等待分配CPU。
  4. Thread.join:让当前线程等待指定线程终结。(源码中调用了“Object.wait”,即“释放cpu,释放锁”)
  5. LockSupport.park[1]:(静态方法)释放cpu,保持锁

参考