线程的几种状态.md

2019-11-02  本文已影响0人  zivxia

NEW

/**
  * Thread state for a thread which has not yet started.
*/

线程已经创建,但是还未启动

RUNNABLE

/**
  * 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.
*/

一个正在被执行的线程的状态

BLOCKED

/**
  * 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}.
*/

一个线程因为等待临界区(同步代码块或者同步方法,非java.util.concurrent库中的锁)的锁被阻塞产生的状态,Lock 或者synchronize 关键字产生的状态。或者重进入临界区的锁当调用wait之后

WAITTING

/**
 * 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.
 */

一个线程进入了锁,但是需要等待其他线程执行某些操作。时间不确定,当wait,join,park方法调以及等待Lock或Condition用时,进入waiting状态。前提是这个线程已经拥有锁了。

TIMED_WAITTING

/**
  * 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>
*/

一个线程进入了锁,但是需要等待其他线程执行某些操作。时间确定,通过sleep或wait timeout方法进入的限期等待的状态

TERMINATED

/**
  * Thread state for a terminated thread.
  * The thread has completed execution.
*/

线程已经终止

上一篇下一篇

猜你喜欢

热点阅读