溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java多線程啟動(dòng)為什么調(diào)用的是start()方法而不是run() 方法

發(fā)布時(shí)間:2021-09-18 09:35:47 來源:億速云 閱讀:101 作者:chen 欄目:編程語言

這篇文章主要講解了“Java多線程啟動(dòng)為什么調(diào)用的是start()方法而不是run() 方法”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Java多線程啟動(dòng)為什么調(diào)用的是start()方法而不是run() 方法”吧!

線程的狀態(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),可以看下面這張圖:

Java多線程啟動(dòng)為什么調(diào)用的是start()方法而不是run() 方法

這張圖描述的還是非常詳細(xì)的,結(jié)合這張圖,來說說這幾種狀態(tài)分別代表著什么意思:

1、NEW 表示線程創(chuàng)建成功,但沒有運(yùn)行,在 new Thread 之后,沒有 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é) 束等待,只不過 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 出來的優(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("我是通過繼承 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("我是通過 runnable 方式實(shí)現(xiàn)的~");             }         });         // 啟動(dòng)線程         thread.start();     } }

不管使用哪一種方式,啟動(dòng)線程都是thread.start()方法,如果你做過實(shí)驗(yàn)的話,你會(huì)發(fā)現(xiàn)  thread.run()也可以執(zhí)行,為什么就一定需要調(diào)用thread.start()方法呢?

先說說結(jié)論:首先通過對(duì)象.run()方法可以執(zhí)行方法,但是不是使用的多線程的方式,就是一個(gè)普通的方法,要想實(shí)現(xiàn)多線程的方式,一定需要通過對(duì)象.start()方法。

想要弄明白一個(gè)問題,比較好的辦法就是從源碼入手,我們也從這兩個(gè)方法的源碼開始,先來看看 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".      */      // 沒有初始化,拋出異常     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 方法的源碼也沒幾行代碼,注釋也比較詳細(xì),最主要的是 start0() 方法,這個(gè)后面在解釋。再來看看 run() 方法的源碼:

@Override    public void run() {     // 簡(jiǎn)單的運(yùn)行,不會(huì)新起線程,target 是 Runnable        if (target != null) {            target.run();        }    }

run() 方法的源碼就比較簡(jiǎn)單的,就是一個(gè)普通方法的調(diào)用,這也印證了我們上面的結(jié)論。

接下來我們就來說一說這個(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)說起,看下面這張圖:

Java多線程啟動(dòng)為什么調(diào)用的是start()方法而不是run() 方法

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  來實(shí)現(xiàn)了,start0() 方法自然就表標(biāo)記成了 native。

最后,總結(jié)一下,Java 中實(shí)現(xiàn)真正的多線程是 start 中的 start0() 方法,run() 方法只是一個(gè)普通的方法。

感謝各位的閱讀,以上就是“Java多線程啟動(dòng)為什么調(diào)用的是start()方法而不是run() 方法”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)Java多線程啟動(dòng)為什么調(diào)用的是start()方法而不是run() 方法這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI