public static enum Thread.State extends Enum<Thread.State>線程狀態。
線程能夠處於下列狀態之一:
1.NEW 至今還沒有啓動的線程的狀態。
2.RUNNABLE 可運行線程的線程狀態。
處於可運行狀態的某一線程正在 Java 虛擬機中運行,但它可能正在等待操做系統中的其餘資源,好比處理器。
3.BLOCKED 受阻塞而且正在等待監視器鎖的某一線程的線程狀態。
處於受阻塞狀態的某一線程正在等待監視器鎖,以便進入一個同步的塊/方法,或者在調用 Object.wait 以後再次進入同步的塊/方法。
4.WAITING 某一等待線程的線程狀態。某一線程由於調用下列方法之一而處於等待狀態:html
LockSupport.park
處於等待狀態的線程正等待另外一個線程,以執行特定操做。
例如,已經在某一對象上調用了 Object.wait() 的線程正等待另外一個線程,以便在該對象上調用 Object.notify() 或 Object.notifyAll()。
已經調用了 Thread.join() 的線程正在等待指定線程終止。
5.TIMED_WAITING具備指定等待時間的某一等待線程的線程狀態。某一線程由於調用如下帶有指定正等待時間的方法之一而處於定時等待狀態:java
6.TERMINATED
已終止線程的線程狀態。線程已經結束執行。
注意:在給定時間點上,一個線程只能處於一種狀態。這些狀態是虛擬機狀態,它們並無反映全部操做系統線程狀態。
爲了展示線程在運行時的狀態及其轉換,我畫了下面這個圖this
/** * A thread state. A thread can be in one of the following states: * <ul> * <li>{@link #NEW}<br> * A thread that has not yet started is in this state. * </li> * <li>{@link #RUNNABLE}<br> * A thread executing in the Java virtual machine is in this state. * </li> * <li>{@link #BLOCKED}<br> * A thread that is blocked waiting for a monitor lock * is in this state. * </li> * <li>{@link #WAITING}<br> * A thread that is waiting indefinitely for another thread to * perform a particular action is in this state. * </li> * <li>{@link #TIMED_WAITING}<br> * A thread that is waiting for another thread to perform an action * for up to a specified waiting time is in this state. * </li> * <li>{@link #TERMINATED}<br> * A thread that has exited is in this state. * </li> * </ul> * * <p> * 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 { /** * 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; }
java.lang.Thread.Statespa