java多线程基础:线程的状态

打开jdk源码,在Thread类中找到State枚举类。

 /**
     * 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. *
  • *
  • {@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 */ public enum State { NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED; }

一共有6个状态。

NEW:初始状态,线程被构造,还未执行start()方法。(在构造方法中是这个状态)

RUNNABLE:运行状态,java 中把就绪和运行两种状态都成为运行中。(正常运行中是这个状态)

BLOCKED:堵塞状态,表示线程堵塞于锁.。(等待锁是这个状态)

WAITTING:等待状态,表示线程进入等待需要其他线程唤醒(调用wait后是这个状态)

TIME_WAITTING:超时等待状态,不同于WAITTING,到达指定时间,自动返回。(wait(long)是这个状态)

TERMINATED:终止状态,代表当前线程执行结束。(线程结束)

你可能感兴趣的:(java多线程)