Java線程生命周期及轉(zhuǎn)換過程
前言:
線程的生命周期指的是線程從創(chuàng)建到銷毀的整個(gè)過程,通常情況下線程的生命周期有以下 5 種:
- 初始狀態(tài)
- 可運(yùn)行狀態(tài)
- 運(yùn)行狀態(tài)
- 休眠狀態(tài)
- 終止?fàn)顟B(tài)
它們的狀態(tài)轉(zhuǎn)換如下圖所示:

Java 線程生命周期
Java 線程的生命周期和上面說的生命周期是不同的,它有以下 6 種狀態(tài):
- NEW(初始化狀態(tài))
- RUNNABLE(可運(yùn)行/運(yùn)行狀態(tài))
- BLOCKED(阻塞狀態(tài))
- WAITING(無時(shí)限等待狀態(tài))
- TIMED_WAITING(有時(shí)限等待狀態(tài))
- TERMINATED(終止?fàn)顟B(tài))
我們可以在 Thread 的源碼中可以找到這 6 種狀態(tài),如下所示:

當(dāng)然你也可以使用 Java 代碼,來打印所有的線程狀態(tài),如下代碼所示:
for (Thread.State value : Thread.State.values()) {
System.out.println(value);
}以上程序的執(zhí)行結(jié)果如下圖所示:

生命周期轉(zhuǎn)換
接下來我們聊聊 Java 線程生命周期的轉(zhuǎn)換過程。
1.從 NEW 到 RUNNABLE
當(dāng)我們創(chuàng)建一個(gè)線程的時(shí)候,也就是 new Thread 的時(shí)候,此時(shí)線程是 NEW 狀態(tài),如下代碼所示:
// 創(chuàng)建線程
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// ...
}
});
// 獲取線程狀態(tài)
Thread.State state = thread.getState();
System.out.println(state);以上程序的執(zhí)行結(jié)果如下圖所示:

然而調(diào)用了線程的 start 方法之后,線程的狀態(tài)就從 NEW 變成了 RUNNABLE,
如下代碼所示:
// 創(chuàng)建線程
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// 獲取到當(dāng)前執(zhí)行的線程
Thread currThread = Thread.currentThread();
// 獲取線程狀態(tài)
Thread.State state = currThread.getState();
// 打印線程狀態(tài)
System.out.println(state);
}
});
thread.start();以上程序的執(zhí)行結(jié)果如下圖所示:

2.從 RUNNABLE 到 BLOCKED
當(dāng)線程中的代碼排隊(duì)執(zhí)行 synchronized 時(shí),線程就會(huì)從 RUNNABLE 狀態(tài)變?yōu)?BLOCKED 阻塞狀態(tài)
如下代碼所示:
// 創(chuàng)建線程
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
// 等待 100 毫秒
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("排隊(duì)使用鎖");
synchronized (ThreadStates.class) {
}
}
});
thread.start();
// 讓主線程先得到鎖
synchronized (ThreadStates.class) {
// 獲取線程狀態(tài)
Thread.State state = thread.getState();
// 打印線程狀態(tài)
System.out.println("首次獲取線程狀態(tài):" + state);
// 休眠 1s
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 再次獲取線程狀態(tài)
state = thread.getState();
// 打印線程狀態(tài)
System.out.println("第二次獲取線程狀態(tài):" + state);
}以上程序的執(zhí)行結(jié)果如下圖所示:

當(dāng)線程獲取到 synchronized 鎖之后,就會(huì)從 BLOCKED 狀態(tài)轉(zhuǎn)變?yōu)?RUNNABLE 狀態(tài)。
3.從 RUNNABLE 到 WAITTING
線程調(diào)用 wait() 方法之后,就會(huì)從 RUNNABLE 狀態(tài)變?yōu)?WAITING 無時(shí)限等待狀態(tài),如下所示:
// 創(chuàng)建線程
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
synchronized (this) {
try {
// 線程休眠
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
// 啟動(dòng)線程
thread.start();
// 獲取線程狀態(tài)
Thread.State state = thread.getState();
// 打印線程狀態(tài)
System.out.println("首次獲取線程狀態(tài):" + state);
// 休眠 1s
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 獲取線程狀態(tài)
state = thread.getState();
// 打印線程狀態(tài)
System.out.println("第二次獲取線程狀態(tài):" + state);以上程序的執(zhí)行結(jié)果如下圖所示:

當(dāng)調(diào)用了 notify/notifyAll 方法之后,線程會(huì)從 WAITING 狀態(tài)變成 RUNNABLE 狀態(tài),
如下代碼所示:
Object lock = new Object();
// 創(chuàng)建線程
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
try {
// 線程休眠
lock.wait();
// 獲取當(dāng)前線程狀態(tài)
Thread.State state = Thread.currentThread().getState();
// 打印線程狀態(tài)
System.out.println("獲取線程狀態(tài):" + state);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
// 啟動(dòng)線程
thread.start();
// 獲取線程狀態(tài)
Thread.State state = thread.getState();
// 打印線程狀態(tài)
System.out.println("首次獲取線程狀態(tài):" + state);
// 休眠 1s
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 獲取線程狀態(tài)
state = thread.getState();
// 打印線程狀態(tài)
System.out.println("第二次獲取線程狀態(tài):" + state);
// 喚醒 thread 線程
synchronized (lock) {
lock.notify();
}以上程序的執(zhí)行結(jié)果如下圖所示:

4.從 RUNNABLE 到 TIMED_WATTING
當(dāng)調(diào)用帶超時(shí)時(shí)間的等待方法時(shí),如 sleep(xxx),線程會(huì)從 RUNNABLE 狀態(tài)變成 TIMED_WAITING 有時(shí)限狀態(tài),
如下代碼所示:
// 創(chuàng)建線程
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
// 啟動(dòng)線程
thread.start();
// 獲取線程狀態(tài)
Thread.State state = thread.getState();
// 打印線程狀態(tài)
System.out.println("首次獲取線程狀態(tài):" + state);
// 休眠 1s
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 獲取線程狀態(tài)
state = thread.getState();
// 打印線程狀態(tài)
System.out.println("第二次獲取線程狀態(tài):" + state);以上程序的執(zhí)行結(jié)果如下圖所示:

當(dāng)超過了超時(shí)時(shí)間之后,線程就會(huì)從 TIMED_WAITING 狀態(tài)變成 RUNNABLE 狀態(tài),
實(shí)現(xiàn)代碼如下:
// 創(chuàng)建線程
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
// 獲取當(dāng)前線程狀態(tài)
Thread.State state = Thread.currentThread().getState();
// 打印線程狀態(tài)
System.out.println("獲取線程狀態(tài):" + state);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
// 啟動(dòng)線程
thread.start();
// 獲取線程狀態(tài)
Thread.State state = thread.getState();
// 打印線程狀態(tài)
System.out.println("首次獲取線程狀態(tài):" + state);
// 休眠 1s
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 獲取線程狀態(tài)
state = thread.getState();
// 打印線程狀態(tài)
System.out.println("第二次獲取線程狀態(tài):" + state);以上程序的執(zhí)行結(jié)果如下圖所示:

5.RUNNABLE 到 TERMINATED
線程執(zhí)行完之后,就會(huì)從 RUNNABLE 狀態(tài)變成 TERMINATED 銷毀狀態(tài),如下代碼所示:
// 創(chuàng)建線程
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// 獲取當(dāng)前線程狀態(tài)
Thread.State state = Thread.currentThread().getState();
// 打印線程狀態(tài)
System.out.println("獲取線程狀態(tài):" + state);
}
});
// 啟動(dòng)線程
thread.start();
// 等待 100ms,待線程執(zhí)行完
Thread.sleep(100);
// 獲取線程狀態(tài)
Thread.State state = thread.getState();
// 打印線程狀態(tài)
System.out.println("線程狀態(tài):" + state);以上程序的執(zhí)行結(jié)果如下圖所示:

總結(jié)
Java 中線程的生命周期有 6 種:NEW(初始化狀態(tài))、RUNNABLE(可運(yùn)行/運(yùn)行狀態(tài))、BLOCKED(阻塞狀態(tài))、WAITING(無時(shí)限等待狀態(tài))、TIMED_WAITING(有時(shí)限等待狀態(tài))、TERMINATED(終止?fàn)顟B(tài))。
線程生命周期的轉(zhuǎn)換流程如下圖所示:

到此這篇關(guān)于Java線程生命周期及轉(zhuǎn)換過程的文章就介紹到這了,更多相關(guān)Java線程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis中輸入輸出映射與動(dòng)態(tài)Sql圖文詳解
這篇文章主要給大家介紹了關(guān)于Mybatis中輸入輸出映射與動(dòng)態(tài)Sql的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
RestTemplate報(bào)錯(cuò)400 Bad Request的解決方案
在使用Spring Boot時(shí),若直接通過@Autowired注入RestTemplate可能會(huì)遇到400BadRequest錯(cuò)誤,原因在于Spring Boot官方文檔指出,由于RestTemplate實(shí)例通常需要在使用前進(jìn)行定制,因此Spring Boot不會(huì)自動(dòng)配置單個(gè)RestTemplate Bean2024-11-11
Linux下Java開發(fā)環(huán)境搭建以及第一個(gè)HelloWorld
這篇文章主要介紹了Linux下Java開發(fā)環(huán)境搭建以及第一個(gè)HelloWorld的實(shí)現(xiàn)過程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-09-09
Log4j定時(shí)打印日志及添加模塊名配置的Java代碼實(shí)例
這篇文章主要介紹了Log4j定時(shí)打印日志及添加模塊名配置的Java代碼實(shí)例,Log4j是Apache的一個(gè)開源Java日志項(xiàng)目,需要的朋友可以參考下2016-01-01
Java實(shí)現(xiàn)差分?jǐn)?shù)組的示例詳解
差分?jǐn)?shù)組是由原數(shù)組進(jìn)化而來,值為原數(shù)組當(dāng)前位置值減去上一個(gè)位置的值。本文將通過例題詳解如何利用Java實(shí)現(xiàn)差分?jǐn)?shù)組,需要的可以參考一下2022-06-06
Spring中的@Transactional事務(wù)失效場(chǎng)景解讀
這篇文章主要介紹了Spring中的@Transactional事務(wù)失效場(chǎng)景解讀,如果Transactional注解應(yīng)用在非public 修飾的方法上,Transactional將會(huì)失效此方法會(huì)檢查目標(biāo)方法的修飾符是否為 public,不是 public則不會(huì)獲取@Transactional 的屬性配置信息,需要的朋友可以參考下2023-12-12

