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

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

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

生命周期轉(zhuǎn)換
接下來(lái)我們聊聊 Java 線程生命周期的轉(zhuǎn)換過(guò)程。
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 無(wú)時(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)超過(guò)了超時(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 銷(xiāo)毀狀態(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(無(wú)時(shí)限等待狀態(tài))、TIMED_WAITING(有時(shí)限等待狀態(tài))、TERMINATED(終止?fàn)顟B(tài))。
線程生命周期的轉(zhuǎn)換流程如下圖所示:

到此這篇關(guān)于Java線程生命周期及轉(zhuǎn)換過(guò)程的文章就介紹到這了,更多相關(guān)Java線程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一篇超詳細(xì)的Spring Boot對(duì)jdbc支持的文章
JdbcTemplate 是在JDBC API基礎(chǔ)上提供了更抽象的封裝,并提供了基于方法注解的事務(wù)管理能力。 通過(guò)使用SpringBoot自動(dòng)配置功能并代替我們自動(dòng)配置beans,下面給大家介紹spring boot中使用JdbcTemplate相關(guān)知識(shí),一起看看吧2021-07-07
Mybatis Plus 代碼生成器的實(shí)現(xiàn)
這篇文章主要介紹了Mybatis Plus 代碼生成器的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Java銀行取錢(qián)線程安全問(wèn)題實(shí)例分析
這篇文章主要介紹了Java銀行取錢(qián)線程安全問(wèn)題,結(jié)合具體實(shí)例形式分析了java使用線程操作模擬銀行取錢(qián)的相關(guān)安全問(wèn)題,需要的朋友可以參考下2019-09-09
Spring+SpringMVC+MyBatis深入學(xué)習(xí)及搭建(二)之MyBatis原始Dao開(kāi)發(fā)和mapper代理開(kāi)發(fā)
這篇文章主要介紹了Spring+SpringMVC+MyBatis深入學(xué)習(xí)及搭建(二)之MyBatis原始Dao開(kāi)發(fā)和mapper代理開(kāi)發(fā),需要的朋友可以參考下2017-05-05
Java中獲取時(shí)間戳的三種方式對(duì)比實(shí)現(xiàn)
這篇文章主要介紹了Java中獲取時(shí)間戳的三種方式對(duì)比實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
淺析Spring IOC bean為什么默認(rèn)是單例
單例的意思就是說(shuō)在 Spring IoC 容器中只會(huì)存在一個(gè) bean 的實(shí)例,無(wú)論一次調(diào)用還是多次調(diào)用,始終指向的都是同一個(gè) bean 對(duì)象,本文小編將和大家一起分析Spring IOC bean為什么默認(rèn)是單例,需要的朋友可以參考下2023-12-12
MyBatis-Plus工具使用之EntityWrapper解析
這篇文章主要介紹了MyBatis-Plus工具使用之EntityWrapper解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

