Java 多線程啟動(dòng)為什么調(diào)用 start() 方法而不是 run() 方法?
多線程在工作中多多少少會(huì)用到,啟動(dòng)多線程調(diào)用的是 start() 方法,而不是 run() 方法,這是為什么呢?
在探討這個(gè)問(wèn)題之前,先來(lái)了解(復(fù)習(xí))一些多線程的基礎(chǔ)知識(shí)~
線程的狀態(tài)
Java 中,定義了 6 種線程狀態(tài),在 Thread 類可以找到:
- // 為了節(jié)約空間,我刪除了注釋
- public enum State {
- NEW,//初始狀態(tài)
- RUNNABLE,//運(yùn)行狀態(tài)
- BLOCKED,// 阻塞狀態(tài)
- WAITING,//等待狀態(tài)
- TIMED_WAITING,//超時(shí)等待狀態(tài)
- TERMINATED;//終止?fàn)顟B(tài)
- }
這 6 種狀態(tài)之間的關(guān)聯(lián),可以看下面這張圖:
這張圖描述的還是非常詳細(xì)的,結(jié)合這張圖,來(lái)說(shuō)說(shuō)這幾種狀態(tài)分別代表著什么意思:
1、NEW 表示線程創(chuàng)建成功,但沒(méi)有運(yùn)行,在 new Thread 之后,沒(méi)有 start 之前,線程都處于 NEW 狀態(tài);
2、RUNNABLE 表示線程正在運(yùn)行中,當(dāng)我們運(yùn)行 strat 方法,子線程被創(chuàng)建成功之后,子線程的狀態(tài)變成 RUNNABLE;
3、TERMINATED 表示線程已經(jīng)運(yùn)行結(jié)束,子線程運(yùn)行完成、被打斷、被中止,狀態(tài)都會(huì)從 RUNNABLE 變成 TERMINATED;
4、BLOCKED 表示線程被阻塞,如果線程正好在等待獲得 monitor lock 鎖,比如在等待進(jìn)入 synchronized 修飾的代碼塊或方法時(shí),會(huì)從 RUNNABLE 變成 BLOCKED;
5、 WAITING 和 TIMED_WAITING 都表示等待,現(xiàn)在在遇到 Object#wait、Thread#join、 LockSupport#park 這些方法時(shí),線程就會(huì)等待另一個(gè)線程執(zhí)行完特定的動(dòng)作之后,才能結(jié) 束等待,只不過(guò) TIMED_WAITING 是帶有等待時(shí)間的;
優(yōu)先級(jí)
優(yōu)先級(jí)代表線程執(zhí)行的機(jī)會(huì)的大小,優(yōu)先級(jí)高的可能先執(zhí)行,低的可能后執(zhí)行。
在 Java 源碼中,優(yōu)先級(jí)從低到高分別是 1 到 10,線程默認(rèn) new 出來(lái)的優(yōu)先級(jí)都是 5,源碼如下:
- /**
- * The minimum priority that a thread can have.
- */
- public final static int MIN_PRIORITY = 1;
- /**
- * The default priority that is assigned to a thread.
- */
- public final static int NORM_PRIORITY = 5;
- /**
- * The maximum priority that a thread can have.
- */
- public final static int MAX_PRIORITY = 10;
線程的創(chuàng)建方式
我們創(chuàng)建多線程有兩種方式,一種是繼承 Thread 類,另一種是實(shí)現(xiàn) Runnable 接口。兩種方式的使用,如下所示:
1、繼承 Thread,成為 Thread 的子類
- public class MyThread extends Thread{
- @Override
- public void run() {
- System.out.println("我是通過(guò)繼承 Thread 類實(shí)現(xiàn)的~");
- }
- public static void main(String[] args) {
- MyThread thread = new MyThread();
- // 啟動(dòng)線程
- thread.start();
- }
- }
2、實(shí)現(xiàn) Runnable 接口
- public class MyThread1 {
- public static void main(String[] args) {
- Thread thread = new Thread(new Runnable() {
- @Override
- public void run() {
- System.out.println("我是通過(guò) runnable 方式實(shí)現(xiàn)的~");
- }
- });
- // 啟動(dòng)線程
- thread.start();
- }
- }
不管使用哪一種方式,啟動(dòng)線程都是thread.start()方法,如果你做過(guò)實(shí)驗(yàn)的話,你會(huì)發(fā)現(xiàn) thread.run()也可以執(zhí)行,為什么就一定需要調(diào)用thread.start()方法呢?
先說(shuō)說(shuō)結(jié)論:首先通過(guò)對(duì)象.run()方法可以執(zhí)行方法,但是不是使用的多線程的方式,就是一個(gè)普通的方法,要想實(shí)現(xiàn)多線程的方式,一定需要通過(guò)對(duì)象.start()方法。
想要弄明白一個(gè)問(wèn)題,比較好的辦法就是從源碼入手,我們也從這兩個(gè)方法的源碼開(kāi)始,先來(lái)看看 start 方法的源碼:
- public synchronized void start() {
- /**
- * This method is not invoked for the main method thread or "system"
- * group threads created/set up by the VM. Any new functionality added
- * to this method in the future may have to also be added to the VM.
- *
- * A zero status value corresponds to state "NEW".
- */
- // 沒(méi)有初始化,拋出異常
- if (threadStatus != 0)
- throw new IllegalThreadStateException();
- /* Notify the group that this thread is about to be started
- * so that it can be added to the group's list of threads
- * and the group's unstarted count can be decremented. */
- group.add(this);
- // 是否啟動(dòng)的標(biāo)識(shí)符
- boolean started = false;
- try {
- // start0() 是啟動(dòng)多線程的關(guān)鍵
- // 這里會(huì)創(chuàng)建一個(gè)新的線程,是一個(gè) native 方法
- // 執(zhí)行完成之后,新的線程已經(jīng)在運(yùn)行了
- start0();
- // 主線程執(zhí)行
- started = true;
- } finally {
- try {
- if (!started) {
- group.threadStartFailed(this);
- }
- } catch (Throwable ignore) {
- /* do nothing. If start0 threw a Throwable then
- it will be passed up the call stack */
- }
- }
- }
start 方法的源碼也沒(méi)幾行代碼,注釋也比較詳細(xì),最主要的是 start0() 方法,這個(gè)后面在解釋。再來(lái)看看 run() 方法的源碼:
- @Override
- public void run() {
- // 簡(jiǎn)單的運(yùn)行,不會(huì)新起線程,target 是 Runnable
- if (target != null) {
- target.run();
- }
- }
run() 方法的源碼就比較簡(jiǎn)單的,就是一個(gè)普通方法的調(diào)用,這也印證了我們上面的結(jié)論。
接下來(lái)我們就來(lái)說(shuō)一說(shuō)這個(gè) start0() 這個(gè)方法,這個(gè)是真正實(shí)現(xiàn)多線程的關(guān)鍵,start0() 代碼如下:
- private native void start0();
start0 被標(biāo)記成 native ,也就是本地方法,并不需要我們?nèi)?shí)現(xiàn)或者了解,**為什么 start0() 會(huì)標(biāo)記成 native **?
這個(gè)要從 Java 跨平臺(tái)說(shuō)起,看下面這張圖:
start() 方法調(diào)用 start0() 方法后,該線程并不一定會(huì)立馬執(zhí)行,只是將線程變成了可運(yùn)行狀態(tài)(NEW ---> RUNNABLE)。具體什么時(shí)候執(zhí)行,取決于 CPU ,由 CPU 統(tǒng)一調(diào)度。
我們又知道 Java 是跨平臺(tái)的,可以在不同系統(tǒng)上運(yùn)行,每個(gè)系統(tǒng)的 CPU 調(diào)度算法不一樣,所以就需要做不同的處理,這件事情就只能交給 JVM 來(lái)實(shí)現(xiàn)了,start0() 方法自然就表標(biāo)記成了 native。
最后,總結(jié)一下,Java 中實(shí)現(xiàn)真正的多線程是 start 中的 start0() 方法,run() 方法只是一個(gè)普通的方法。