Java 多執行緒啟動為什麼調用 start() 方法而不是 run() 方法?
多執行緒在工作中多多少少會用到,我們知道啟動多執行緒調用的是 start() 方法,而不是 run() 方法,你知道原因嗎?
在探討這個問題之前,我們先來了解一些多執行緒的基礎知識~
執行緒的狀態
Java 中,定義了 6 種執行緒狀態,在 Thread 類可以找到:
// 為了節約空間,我刪除了注釋
public enum State {
NEW,//初始狀態
RUNNABLE,//運行狀態
BLOCKED,// 阻塞狀態
WAITING,//等待狀態
TIMED_WAITING,//超時等待狀態
TERMINATED;//終止狀態
}
這 6 種狀態之間的關聯,可以看下面這張圖:
這張圖描述的還是非常詳細的,結合這張圖,來說說這幾種狀態分別代表著什麼意思:
- 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,源碼如下:
/**
* 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 的子類
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 介面
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 方法的源碼:
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() 方法的源碼:
@Override
public void run() {
// 簡單的運行,不會新起執行緒,target 是 Runnable
if (target != null) {
target.run();
}
}
run() 方法的源碼就比較簡單的,就是一個普通方法的調用,這也印證了我們上面的結論。
接下來我們就來說一說這個 start0() 這個方法,這個是真正實現多執行緒的關鍵,start0() 程式碼如下:
private native void start0();
start0 被標記成 native ,也就是本地方法,並不需要我們去實現或者了解,**為什麼 start0() 會標記成 native **?
這個要從 Java 跨平台說起,看下面這張圖:
start() 方法調用 start0() 方法後,該執行緒並不一定會立馬執行,只是將執行緒變成了可運行狀態。具體什麼時候執行,取決於 CPU ,由 CPU 統一調度。
我們又知道 Java 是跨平台的,可以在不同系統上運行,每個系統的 CPU 調度演算法不一樣,所以就需要做不同的處理,這件事情就只能交給 JVM 來實現了,start0() 方法自然就表標記成了 native。
最後,總結一下,Java 中實現真正的多執行緒是 start 中的 start0() 方法,run() 方法只是一個普通的方法。