1. 程式人生 > >Java 多執行緒啟動為什麼呼叫 start() 方法而不是 run() 方法?

Java 多執行緒啟動為什麼呼叫 start() 方法而不是 run() 方法?

多執行緒在工作中多多少少會用到,我們知道啟動**多執行緒呼叫的是 start() 方法,而不是 run() 方法,你知道原因嗎?** 在探討這個問題之前,我們先來了解一些多執行緒的基礎知識~ #### 執行緒的狀態 Java 中,定義了 6 種執行緒狀態,在 Thread 類可以找到: ```java // 為了節約空間,我刪除了註釋 public enum State { NEW,//初始狀態 RUNNABLE,//執行狀態 BLOCKED,// 阻塞狀態 WAITING,//等待狀態 TIMED_WAITING,//超時等待狀態 TERMINATED;//終止狀態 } ``` 這 6 種狀態之間的關聯,可以看下面這張圖: ![圖片來源網路](https://user-gold-cdn.xitu.io/2020/5/12/17207a760f10d13e?w=716&h=474&f=png&s=136367) 這張圖描述的還是非常詳細的,結合這張圖,來說說這幾種狀態分別代表著什麼意思: - 1、**NEW 表示執行緒建立成功,但沒有執行**,在 new Thread 之後,沒有 start 之前,執行緒都處於 NEW 狀態; - 2、**RUNNABLE 表示執行緒正在執行中**,當我們執行 strat 方法,子執行緒被建立成功之後,子執行緒的狀態變成 RUNNABLE; - 3、**TERMINATED 表示執行緒已經執行結束**,子執行緒執行完成、被打斷、被中止,狀態都會從 RUNNABLE 變成 TERMINATED; - 4、**BLOCKED 表示執行緒被阻塞**,如果執行緒正好在等待獲得 monitor lock 鎖,比如在等待進入 synchronized 修飾的程式碼塊或方法時,會從 RUNNABLE 變成 BLOCKED; - 5、 **WAITING 和 TIMED_WAITING 都表示等待**,現在在遇到 Object#wait、Thread#join、 LockSupport#park 這些方法時,執行緒就會等待另一個執行緒執行完特定的動作之後,才能結 束等待,只不過 TIMED_WAITING 是帶有等待時間的; #### 優先順序 優先順序代表執行緒執行的機會的大小,優先順序高的可能先執行,低的可能後執行。 在 Java 原始碼中,優先順序從低到高分別是 1 到 10,執行緒預設 new 出來的優先順序都是 5,原始碼如下: ```java /** * 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; ``` #### 執行緒得建立方式 我們建立多執行緒有兩種方式,一種是繼承 Thread 類,另一種是實現 Runnable 介面。兩種方式的使用,如下所示: #### 1、繼承 Thread,成為 Thread 的子類 ```java public class MyThread extends Thread{ @Override public void run() { System.out.println("我是通過繼承 Thread 類實現的~"); } public static void main(String[] args) { MyThread thread = new MyThread(); // 啟動執行緒 thread.start(); } } ``` #### 2、實現 Runnable 介面 ```java public class MyThread1 { public static void main(String[] args) { Thread thread = new Thread(new Runnable() { @Override public void run() { System.out.println("我是通過 runnable 方式實現的~"); } }); // 啟動執行緒 thread.start(); } } ``` 不管使用哪一種方式,啟動執行緒都是`thread.start()`方法,如果你做過實驗的話,你會發現 **`thread.run()`也可以執行,為什麼就一定需要呼叫`thread.start()`方法呢**? 先說說結論:**首先通過`物件.run()`方法可以執行方法,但是不是使用的多執行緒的方式,就是一個普通的方法,要想實現多執行緒的方式,一定需要通過`物件.start()`方法**。 **想要弄明白一個問題,最好的辦法就是從原始碼入手**,我們也從這兩個方法的原始碼開始,先來看看 start 方法的原始碼: ```java 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); // 是否啟動的識別符號 boolean started = false; try { // start0() 是啟動多執行緒的關鍵 // 這裡會建立一個新的執行緒,是一個 native 方法 // 執行完成之後,新的執行緒已經在運行了 start0(); // 主執行緒執行 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 方法的原始碼也沒幾行程式碼,註釋也比較詳細,最主要的是 start0() 方法,這個後面在解釋。再來看看 run() 方法的原始碼: ```java @Override public void run() { // 簡單的執行,不會新起執行緒,target 是 Runnable if (target != null) { target.run(); } } ``` run() 方法的原始碼就比較簡單的,就是一個普通方法的呼叫,這也印證了我們上面的結論。 接下來我們就來說一說這個 start0() 這個方法,這個是真正實現多執行緒的關鍵,start0() 程式碼如下: ```java private native void start0(); ``` start0 被標記成 native ,也就是本地方法,並不需要我們去實現或者瞭解,**為什麼 start0() 會標記成 native **? 這個要從 Java 跨平臺說起,看下面這張圖: ![圖片來源牛客網](https://user-gold-cdn.xitu.io/2020/5/12/17207a7611966d87?w=1171&h=406&f=png&s=312909) start() 方法呼叫 start0() 方法後,該執行緒並不一定會立馬執行,只是將執行緒變成了可執行狀態。具體什麼時候執行,取決於 CPU ,由 CPU 統一排程。 我們又知道 Java 是跨平臺的,可以在不同系統上執行,每個系統的 CPU 排程演算法不一樣,所以就需要做不同的處理,這件事情就只能交給 JVM 來實現了,start0() 方法自然就表標記成了 native。 **最後**,總結一下,Java 中實現真正的多執行緒是 start 中的 start0() 方法,run() 方法只是一個普通的方法。 ![](https://user-gold-cdn.xitu.io/2020/5/12/17207a8001c90253?w=647&h=385&f=png&s