java 线程的状态

介绍:Java中线程的状态分为6种。Thread类里面定义了枚举类State,里面详细的介绍了6种状态。如下(直接copy的 小小的用百度翻译了一下,(●'◡'●)):

NEW、RUNNABLE、BLOCKED、WAITING、TIMED_WAITING、TERMINATED

/**
     * A thread state.  A thread can be in one of the following states:
     * 
    *
  • {@link #NEW}
    * A thread that has not yet started is in this state. * 尚未启动的线程处于此状态。 *
  • *
  • {@link #RUNNABLE}
    * A thread executing in the Java virtual machine is in this state. * Java虚拟机中执行的线程处于此状态。 *
  • *
  • {@link #BLOCKED}
    * A thread that is blocked waiting for a monitor lock is in this state. * 等待监视器锁定被阻止的线程处于此状态。 *
  • *
  • {@link #WAITING}
    * A thread that is waiting indefinitely for another thread to * perform a particular action is in this state. * 无限期等待另一线程执行特定操作的线程处于此状态。 *
  • *
  • {@link #TIMED_WAITING}
    * A thread that is waiting for another thread to perform an action * for up to a specified waiting time is in this state. * 在指定等待时间内等待另一线程执行操作的线程处于此状态。 *
  • *
  • {@link #TERMINATED}
    * A thread that has exited is in this state. * 已退出的线程处于此状态。 *
  • *
* *

* A thread can be in only one state at a given point in time. * These states are virtual machine states which do not reflect * any operating system thread states. * 一个线程在给定时间点只能处于一种状态。这些状态是不反映任何操作系统线程状态的虚拟机状态。 * * @since 1.5 * @see #getState */

实例: 下面我们就一一来验证这些状态:

NEW:初始状态 - 新创建了一个线程对象,但还没有调用start()方法。

java 线程的状态_第1张图片

 

RUNNABLE: 后面深入继续讲解这个的

java 线程的状态_第2张图片

BLOCKED:阻塞 等待监视器锁定被阻止的线程处于此状态。 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(java)