文章目錄
- 從原始碼看Thread中定義的六種執行緒狀態
- 執行緒狀態的變化
- 1.執行緒正常運行結束的狀態變化
- 2.執行緒執行程序中等待一段時間再繼續執行結束的狀態變化
- 3.執行緒執行程序中等待其他執行緒被阻塞,然后繼續執行到結束的狀態變化
從原始碼看Thread中定義的六種執行緒狀態
Thread類位于java.lang包下,在Thread類中有一個列舉型別State,State中定義了執行緒的六種狀態,原始碼如下:
java.lang.Thread.State
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;
}
6中執行緒狀態:

- 1、NEW
尚未啟動的執行緒的執行緒狀態, - 2、RUNNABLE
可運行執行緒的執行緒狀態,
可運行的執行緒狀態正在Java虛擬機中執行,但它可能等待作業系統的其他資源如處理器, - 3、BLOCKED
執行緒阻塞等待監視器鎖定的執行緒狀態,
處于synchronized同步代碼塊或方法中被阻塞, - 4、WAITING
等待執行緒的執行緒狀態,
處于等待狀態的執行緒正在等待另一個執行緒執行特定的操作, - 5、TIMED_WAITING
具有指定等待時間的等待執行緒的執行緒狀態, - 6、TERMINATED
終止執行緒的執行緒狀態,
執行緒正常完成執行或者出現例外,
注:執行緒在給定時間點只能處于一種狀態,這些狀態是虛擬機狀態,不反映任何作業系統執行緒狀態,
呼叫getState方法可以得到當前執行緒的狀態,
public class ThreadTest {
public static void main(String[] args) {
Thread thread1 = new MyThread();
System.out.println("thread1的執行緒狀態:" + thread1.getState());
thread1.start();
System.out.println("thread1的執行緒狀態:" + thread1.getState());
}
}
class MyThread extends Thread{
@Override
public void run() {
System.out.println(this.getName() + " is running,,,");
}
}

執行緒狀態的變化

1.執行緒正常運行結束的狀態變化
public class ThreadState {
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("當前執行緒正在執行-------");
System.out.println("執行緒正在執行時的執行緒狀態:" + Thread.currentThread().getState().toString());
}
});
System.out.println("創建執行緒后,呼叫start前的執行緒狀態:" + thread1.getState().toString());
thread1.start();
Thread.sleep(2000L); // 等待thread1執行結束,再看狀態
System.out.println("---等待兩秒,執行緒執行結束后的狀態:" + thread1.getState().toString());
//thread1.start(); //當執行緒終止之后,再進行呼叫,會拋出IllegalThreadStateException例外
}
}

程式運行結果顯示:
- 當一個執行緒被創建時(還未呼叫start方法)處于NEW狀態;
- 呼叫start方法后,執行緒開始執行,此時執行緒處于RUNNABLE狀態;
- 執行緒繼續執行,一段時間后,執行緒正常執行結束,此時執行緒處于TERMINATED狀態,
(新建 -> 運行 -> 終止,NEW-> RUNNABLE-> TERMINATED)
注意:當執行緒執行結束之后,再進行呼叫,會拋出IllegalThreadStateException例外

2.執行緒執行程序中等待一段時間再繼續執行結束的狀態變化
public class ThreadState {
public static void main(String[] args) throws InterruptedException {
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("當前執行緒正在執行-------,需要花費兩秒時間------");
try { // 將執行緒2移動到等待狀態,2s后自動喚醒
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("執行緒正在執行時的執行緒狀態:" + Thread.currentThread().getState().toString());
}
});
System.out.println("創建執行緒后,呼叫start前的執行緒狀態:" + thread2.getState().toString());
thread2.start();
Thread.sleep(200L); // 等待200毫秒,再看狀態
System.out.println("等待200毫秒(執行緒還沒執行結束),執行緒的狀態:" + thread2.getState().toString());
Thread.sleep(3000L); // 再等待3秒,讓thread2執行完畢,再看狀態
System.out.println("等待3秒(執行緒執行結束),執行緒的狀態:" + thread2.getState().toString());
}
}

程式運行結果顯示:
- 當一個執行緒被創建時(還未呼叫start方法)處于NEW狀態;
- 呼叫start方法后,執行緒開始執行,此時執行緒處于RUNNABLE狀態;
- 當執行緒運行程序中需要等待一段時間時,執行緒進入TIMED_WAITING狀態
- 執行緒繼續執行,一段時間后,執行緒正常執行結束,此時執行緒處于TERMINATED狀態,
(新建 -> 運行 -> 等待 -> 運行 -> 終止,NEW-> RUNNABLE->TIMED_WAITING->RUNNABLE-> TERMINATED)
3.執行緒執行程序中等待其他執行緒被阻塞,然后繼續執行到結束的狀態變化
public class ThreadState {
public static void main(String[] args) throws InterruptedException {
Thread thread3 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (ThreadState.class) {
System.out.println("當前執行緒正在執行-------");
System.out.println("執行緒正在執行時的執行緒狀態:" + Thread.currentThread().getState().toString());
}
}
});
synchronized (ThreadState.class) {
System.out.println("沒呼叫start方法,thread3當前狀態:" + thread3.getState().toString());
thread3.start();
System.out.println("呼叫start方法,thread3當前狀態:" + thread3.getState().toString());
Thread.sleep(200L);
System.out.println("等待200毫秒,執行緒遇到鎖被堵塞時的狀態:" + thread3.getState().toString());
}
Thread.sleep(3000L); // 再等待3秒,讓thread3執行完畢,再看狀態
System.out.println("等待3秒,執行緒搶到鎖并執行結束時的狀態:" + thread3.getState().toString());
}
}
程式運行結果顯示:
- 當一個執行緒被創建時(還未呼叫start方法)處于NEW狀態;
- 呼叫start方法后,執行緒開始執行,此時執行緒處于RUNNABLE狀態;
- 當執行緒遇到鎖被堵塞時執行緒進入BLOCKED狀態
- 執行緒繼續執行,一段時間后,執行緒正常執行結束,此時執行緒處于TERMINATED狀態,
(新建 -> 運行 -> 阻塞 -> 運行 -> 終止,NEW-> RUNNABLE->BLOCKED->RUNNABLE-> TERMINATED)

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/400605.html
標籤:其他
上一篇:關于補碼原理機制詳解
