線程啟動原理

Java多線程,皆始于Thread。Thread是多線程的根,每一個線程的開啟都始于Thread的start()方法。那么線程是如何被開啟,run方法是如何被執(zhí)行的呢?先上圖:


線程相關類圖.png

這張圖在今后的幾個章節(jié)都會用到,其中只展示了部分關鍵方法。本文主要關注Thread類。

我們都知道啟動一個線程,必須調(diào)用一個Thread的start()方法。在面試時經(jīng)??赡軙粏柕絪tart()和run()方法的區(qū)別,為什么一定要用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開始執(zhí)行。由JVM調(diào)用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、結(jié)果是 調(diào)用start方法的當前線程 和 執(zhí)行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、多次啟動線程永遠不合法。 特別是,線程一旦完成執(zhí)行就不會重新啟動。
     * 
     * @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創(chuàng)建/設置的main方法線程或“system”組線程,不會調(diào)用此方法。 
         *    未來添加到此方法的任何新功能可能也必須添加到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、通知組該線程即將啟動,以便將其添加到線程組的列表中,
         *    并且減少線程組的未啟動線程數(shù)遞減。
         * 
         * */
        group.add(this);

        boolean started = false;
        try {
            //7、調(diào)用native方法,底層開啟異步線程,并調(diào)用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,它將被傳遞給調(diào)用堆棧。
                 */
            }
        }
    }

 //native方法,JVM創(chuàng)建并啟動線程,并調(diào)用run方法
 private native void start0();

對于源碼中的注釋并沒有省略,都進行了翻譯,可以更好的理解整個啟動過程。其中有幾個需要注意的點:

  1. start方法用synchronized修飾,為同步方法;
  2. 雖然為同步方法,但不能避免多次調(diào)用問題,用threadStatus來記錄線程狀態(tài),如果線程被多次start會拋出異常;threadStatus的狀態(tài)由JVM控制。
  3. 使用Runnable時,主線程無法捕獲子線程中的異常狀態(tài)。線程的異常,應在線程內(nèi)部解決。

看完start()方法,線程的啟動邏輯已經(jīng)比較清楚,要探究更底層的原理就需要探究native方法start0()了。

多線程系列目錄(不斷更新中):
線程啟動原理
線程中斷機制
多線程實現(xiàn)方式
FutureTask實現(xiàn)原理
線程池之ThreadPoolExecutor概述
線程池之ThreadPoolExecutor使用
線程池之ThreadPoolExecutor狀態(tài)控制
線程池之ThreadPoolExecutor執(zhí)行原理
線程池之ScheduledThreadPoolExecutor概述
線程池的優(yōu)雅關閉實踐

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 該文章轉(zhuǎn)自:http://blog.csdn.net/evankaka/article/details/44153...
    加來依藍閱讀 7,464評論 3 87
  • Java多線程學習 [-] 一擴展javalangThread類 二實現(xiàn)javalangRunnable接口 三T...
    影馳閱讀 3,105評論 1 18
  • 本文主要講了java中多線程的使用方法、線程同步、線程數(shù)據(jù)傳遞、線程狀態(tài)及相應的一些線程函數(shù)用法、概述等。 首先講...
    李欣陽閱讀 2,584評論 1 15
  • 掐指一算,作為老師已經(jīng)有十幾個年頭了。 曾記得剛畢業(yè)時的“雄心壯志”要成為名師,為此拿...
    德源教育王麗英閱讀 344評論 1 3
  • 生活在于發(fā)現(xiàn)美的存在。
    凜冬將至2018閱讀 188評論 0 1

友情鏈接更多精彩內(nèi)容