執行緒的狀態
Thread.State
首先看JDK中的程式碼: java.lang.Thread.State
/** * 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. * * @since1.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; }
1、新建(New)
新建立了一個執行緒物件,還未呼叫start()方法。
2、就緒(Runnable)
執行緒物件建立後,其他執行緒(比如main執行緒)呼叫了該物件的start()方法。該狀態的執行緒位於可執行執行緒池中,等待被執行緒排程選中 獲取cpu 的使用權 。
3、執行中(Running,執行緒狀態中並沒有這一狀態,但是實際執行中是有的)
可執行狀態(runnable)的執行緒獲得了cpu 時間片(timeslice) ,執行程式程式碼。
4、限期等待(Timed Waiting)
也可以稱作 TIMED_WAITING(有等待時間的等待狀態)。
執行緒主動呼叫以下方法:
Thread.sleep方法;
Object的wait方法,帶有時間;
Thread.join方法,帶有時間;
LockSupport的parkNanos方法,帶有時間。
5、無限期等待(Waiting)
執行中(Running)的執行緒執行了以下方法:
Object的wait方法,並且沒有使用timeout引數;
Thread的join方法,沒有使用timeout引數;
LockSupport的park方法;
Conditon的await方法。
6、阻塞(Blocked)
阻塞狀態是指執行緒因為某種原因放棄了cpu 使用權,暫時停止執行。直到執行緒進入可執行(runnable)狀態,才有機會再次獲得cpu timeslice 轉到執行(running)狀態。阻塞的情況分兩種:
同步阻塞:執行(running)的執行緒進入了一個synchronized方法,若該同步鎖被別的執行緒佔用,則JVM會把該執行緒放入鎖池(lock pool)中。
其他阻塞:執行(running)的執行緒發出了I/O請求時,JVM會把該執行緒置為阻塞狀態。當I/O處理完畢時,執行緒重新轉入可執行(runnable)狀態。
7、結束(Terminated)
執行緒run()、main() 方法執行結束,或者因異常退出了run()方法,則該執行緒結束生命週期。
執行緒狀態探祕
jstack檢視執行緒狀態
jstack -l <pid>
即可察看執行緒狀態,如何使用呢?
隨便寫一個死迴圈看一下
public class TestThreadState { public static void main(String[] args) { for (; ; ) { } } }
ps -ef|grep TestThreadState
,找到對應的pid,jstack -l <pid>即可,如果未輸出執行緒資訊,可以嘗試使用-F引數來強制輸出。
"main" #1 prio=5 os_prio=31 tid=0x00007f8194801800 nid=0x1603 runnable [0x000070000a9b4000] java.lang.Thread.State: RUNNABLE at org.java.bin.TestThreadState.main(TestThreadState.java:12)