多線程在工做中多多少少會用到,咱們知道啓動多線程調用的是 start() 方法,而不是 run() 方法,你知道緣由嗎?java
在探討這個問題以前,咱們先來了解一些多線程的基礎知識~算法
Java 中,定義了 6 種線程狀態,在 Thread 類能夠找到:網絡
// 爲了節約空間,我刪除了註釋
public enum State {
NEW,//初始狀態
RUNNABLE,//運行狀態
BLOCKED,// 阻塞狀態
WAITING,//等待狀態
TIMED_WAITING,//超時等待狀態
TERMINATED;//終止狀態
}
複製代碼
這 6 種狀態之間的關聯,能夠看下面這張圖:多線程
這張圖描述的仍是很是詳細的,結合這張圖,來講說這幾種狀態分別表明着什麼意思:ide
優先級表明線程執行的機會的大小,優先級高的可能先執行,低的可能後執行。this
在 Java 源碼中,優先級從低到高分別是 1 到 10,線程默認 new 出來的優先級都是 5,源碼以下:spa
/** * 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 接口。兩種方式的使用,以下所示:線程
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();
}
}
複製代碼
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()
方法呢?code
先說說結論:首先經過對象.run()
方法能夠執行方法,可是不是使用的多線程的方式,就是一個普通的方法,要想實現多線程的方式,必定須要經過對象.start()
方法。cdn
想要弄明白一個問題,最好的辦法就是從源碼入手,咱們也從這兩個方法的源碼開始,先來看看 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() 方法只是一個普通的方法。