“Java的阻塞、等待及相关方法”的版本间差异

来自Wikioe
跳到导航 跳到搜索
(建立内容为“category:Java”的新页面)
 
无编辑摘要
第1行: 第1行:
[[category:Java]]
[[category:Java]]
== 前言 ==
刚刚正在梳理Java并发部分的知识点,(《Java核心技术卷1》与以前做的笔记),发现这部分出入比较大。所以单开一个词条,用于相关笔记的整理。【2020/10/19 20:50:10】
<br>
== 关于线程状态 ==
线程(进程)的运行状态,最基本的三种:“就绪”、“运行”、“阻塞”。在此基础上,不同的系统进行了扩展(如:“就绪”、“死亡”、“等待”、“挂起”)形成了五状态、六状态、甚至七状态模型。<br/>
而根据其“Thread”源码可以发现,其线程公有六种状态:“New”、“Runnable”、“Blocked”、“Waiting”、“Timed_Waiting”、“Terminated”。
<syntaxhighlight lang="java">
    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;
    }
</syntaxhighlight>
* (网上查到的相关内容,很多都按五种状态分析,可能没看源码,按惯性思维来了,或者1.5以前版本是那样?)
其中:
#“Runnable”可以理解为“就绪”+“运行”;
#“Blocked”:用于标识'''线程等待一个监视器锁''';
#: 如:【?】
## 进入 synchronized 标记的代码块或方法;
## 调用“Object.wait”之后,重入synchronized 标记的代码块或方法;
#“Waiting”:用于标识'''线程等待另一个线程执行特定操作''';
#: 如:
## “Object.wait()”
## “Thread.join()”
## “LockSupport.park()”
#“Timed_Waiting”:'''具有指定等待时间的“Waiting”状态'''
#: 如:(方法带有时间参数)
## “Object.wait(long timeout)”
## “Thread.join(long millis)”
## “Thread.sleep(long millis)”
## “LockSupport.parkNanos(long nanos)”
## “LockSupport.parkUntil(long deadline)”
*(并非像网上所说,“阻塞”分为:“同步阻塞”、“等待阻塞”、“其他阻塞”)
== 相关方法 ==
Object.wait
Thread.sleep
Thread.yeild
Thread.join
LockSupport.park

2020年10月19日 (一) 21:38的版本


前言

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

关于线程状态

线程(进程)的运行状态,最基本的三种:“就绪”、“运行”、“阻塞”。在此基础上,不同的系统进行了扩展(如:“就绪”、“死亡”、“等待”、“挂起”)形成了五状态、六状态、甚至七状态模型。
而根据其“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.5以前版本是那样?)

其中:

  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)”
  • (并非像网上所说,“阻塞”分为:“同步阻塞”、“等待阻塞”、“其他阻塞”)


相关方法

Object.wait Thread.sleep Thread.yeild Thread.join LockSupport.park