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() 方法隻是一個普通的方法。
原文位址
https://www.cnblogs.com/jamaler/p/12876233.html