Java多線程,皆始于Thread。Thread是多線程的根,每一個線程的開啟都始于Thread的start()方法。那么線程是如何被開啟,run方法是如何被執行的呢?先上圖:
線程相關類圖.png
這張圖在今后的幾個章節都會用到,其中只展示了部分關鍵方法。本文主要關注Thread類。
我們都知道啟動一個線程,必須調用一個Thread的start()方法。在面試時經常可能會被問到start()和run()方法的區別,為什么一定要用start()方法才是啟動線程?對比start()方法和run()的源碼一看便知:
/**
* Causes this thread to begin execution; the Java Virtual Machine
* calls the <code>run</code> method of this thread.
*
* 1、start方法將導致this thread開始執行。由JVM調用this thread的run方法。
*
* The result is that two threads are running concurrently: the
* current thread (which returns from the call to the
* <code>start</code> method) and the other thread (which executes its
* <code>run</code> method).
*
* 2、結果是 調用start方法的當前線程 和 執行run方法的另一個線程 同時運行。
*
* It is never legal to start a thread more than once.
* In particular, a thread may not be restarted once it has completed
* execution.
*
* 3、多次啟動線程永遠不合法。 特別是,線程一旦完成執行就不會重新啟動。
*
* @exception IllegalThreadStateException if the thread was already started.
* 如果線程已啟動,則拋出異常。
* @see #run()
* @see #stop()
*/
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.
*
* 4、對于由VM創建/設置的main方法線程或“system”組線程,不會調用此方法。
* 未來添加到此方法的任何新功能可能也必須添加到VM中。
*
* A zero status value corresponds to state "NEW".
* 5、status=0 代表是 status 是 "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.
*
* 6、通知組該線程即將啟動,以便將其添加到線程組的列表中,
* 并且減少線程組的未啟動線程數遞減。
*
* */
group.add(this);
boolean started = false;
try {
//7、調用native方法,底層開啟異步線程,并調用run方法。
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
* 8、忽略異常。 如果start0拋出一個Throwable,它將被傳遞給調用堆棧。
*/
}
}
}
//native方法,JVM創建并啟動線程,并調用run方法
private native void start0();
對于源碼中的注釋并沒有省略,都進行了翻譯,可以更好的理解整個啟動過程。其中有幾個需要注意的點:
- start方法用synchronized修飾,為同步方法;
- 雖然為同步方法,但不能避免多次調用問題,用threadStatus來記錄線程狀態,如果線程被多次start會拋出異常;threadStatus的狀態由JVM控制。
- 使用Runnable時,主線程無法捕獲子線程中的異常狀態。線程的異常,應在線程內部解決。
看完start()方法,線程的啟動邏輯已經比較清楚,要探究更底層的原理就需要探究native方法start0()了。
多線程系列目錄(不斷更新中):
線程啟動原理
線程中斷機制
多線程實現方式
FutureTask實現原理
線程池之ThreadPoolExecutor概述
線程池之ThreadPoolExecutor使用
線程池之ThreadPoolExecutor狀態控制
線程池之ThreadPoolExecutor執行原理
線程池之ScheduledThreadPoolExecutor概述
線程池的優雅關閉實踐