System.exit(status)和Runtime.getRuntime().halt(status)區(qū)別

System.exit(status);
Runtime.getRuntime().halt(status)

System.exit(status)解釋如下:

Terminates the currently running Java Virtual Machine.
The  argument serves as a status code; 
by convention, a nonzero status code indicates abnormal termination 

終止當(dāng)前正在運(yùn)行的Java虛擬機(jī)。參數(shù)作為狀態(tài)代碼,按照慣例,一個(gè)非零狀態(tài)碼表示異常終止。
用線程描述,在多線程情況下,可能更準(zhǔn)確一些。

  1. 調(diào)用方法后,線程會(huì)退出。
  2. 未捕獲的異常被線程拋出,但如果有其他非守護(hù)線程,程序?qū)⒗^續(xù)運(yùn)行。
  3. 反饋狀態(tài)碼,一般在腳本中有用。
  4. 線程退出,還是做一些清理動(dòng)作。
源碼:
/**
     * Terminates the currently running Java virtual machine by initiating its
     * shutdown sequence.  This method never returns normally.  The argument
     * serves as a status code; by convention, a nonzero status code indicates
     * abnormal termination.
     *
     * <p> The virtual machine's shutdown sequence consists of two phases.  In
     * the first phase all registered {@link #addShutdownHook shutdown hooks},
     * if any, are started in some unspecified order and allowed to run
     * concurrently until they finish.  In the second phase all uninvoked
     * finalizers are run if {@link #runFinalizersOnExit finalization-on-exit}
     * has been enabled.  Once this is done the virtual machine {@link #halt
     * halts}.
     *
     * <p> If this method is invoked after the virtual machine has begun its
     * shutdown sequence then if shutdown hooks are being run this method will
     * block indefinitely.  If shutdown hooks have already been run and on-exit
     * finalization has been enabled then this method halts the virtual machine
     * with the given status code if the status is nonzero; otherwise, it
     * blocks indefinitely.
     *
     * <p> The <tt>{@link System#exit(int) System.exit}</tt> method is the
     * conventional and convenient means of invoking this method. <p>
     *
     * @param  status
     *         Termination status.  By convention, a nonzero status code
     *         indicates abnormal termination.
     *
     * @throws SecurityException
     *         If a security manager is present and its <tt>{@link
     *         SecurityManager#checkExit checkExit}</tt> method does not permit
     *         exiting with the specified status
     *
     * @see java.lang.SecurityException
     * @see java.lang.SecurityManager#checkExit(int)
     * @see #addShutdownHook
     * @see #removeShutdownHook
     * @see #runFinalizersOnExit
     * @see #halt(int)
     */
    public void exit(int status) {
        // Make sure we don't try this several times
        synchronized(this) {
            if (!shuttingDown) {
                shuttingDown = true;

                Thread[] hooks;
                synchronized (shutdownHooks) {
                    // create a copy of the hooks
                    hooks = new Thread[shutdownHooks.size()];
                    shutdownHooks.toArray(hooks);
                }

                // Start all shutdown hooks concurrently
                for (Thread hook : hooks) {
                    hook.start();
                }

                // Wait for all shutdown hooks to finish
                for (Thread hook : hooks) {
                    try {
                        hook.join();
                    } catch (InterruptedException ex) {
                        // Ignore, since we are at VM shutdown.
                    }
                }

                // Ensure finalization on exit, if requested
                if (finalizeOnExit) {
                    runFinalization();
                }

                // Get out of here finally...
                nativeExit(status);
            }
        }
    }
Java虛擬機(jī)退出包括兩個(gè)階段:

第一個(gè)階段:會(huì)以某種未指定的順序啟動(dòng)所有已注冊(cè)鉤子,并且允許它們同時(shí)運(yùn)行直至結(jié)束。
第二個(gè)階段:如果已啟用runFinalizersOnExit設(shè)置為true,則運(yùn)行所有未調(diào)用的終結(jié)方法(finalizer方法)。

Runtime.getRuntime().halt(status)解釋如下:

Forcibly terminates the currently running Java virtual machine.  
This method never returns normally. 

現(xiàn)在runtime的halt比較好理解了,他不會(huì)執(zhí)行鉤子函數(shù)和finalizer方法,而是直接退出。

源碼
 /**
     * Forcibly terminates the currently running Java virtual machine.  This
     * method never returns normally.
     *
     * <p> This method should be used with extreme caution.  Unlike the
     * <tt>{@link #exit exit}</tt> method, this method does not cause shutdown
     * hooks to be started and does not run uninvoked finalizers if
     * finalization-on-exit has been enabled.  If the shutdown sequence has
     * already been initiated then this method does not wait for any running
     * shutdown hooks or finalizers to finish their work. <p>
     *
     * @param  status
     *         Termination status.  By convention, a nonzero status code
     *         indicates abnormal termination.  If the <tt>{@link Runtime#exit
     *         exit}</tt> (equivalently, <tt>{@link System#exit(int)
     *         System.exit}</tt>) method has already been invoked then this
     *         status code will override the status code passed to that method.
     *
     * @throws SecurityException
     *         If a security manager is present and its <tt>{@link
     *         SecurityManager#checkExit checkExit}</tt> method does not permit
     *         an exit with the specified status
     *
     * @see #exit
     * @see #addShutdownHook
     * @see #removeShutdownHook
     * @since 1.3
     */
    public void halt(int status) {
        nativeExit(status);
    }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 一.線程安全性 線程安全是建立在對(duì)于對(duì)象狀態(tài)訪問(wèn)操作進(jìn)行管理,特別是對(duì)共享的與可變的狀態(tài)的訪問(wèn) 解釋下上面的話: ...
    黃大大吃不胖閱讀 864評(píng)論 0 3
  • 一,如何正確的關(guān)閉游戲服務(wù)器 1,最簡(jiǎn)單粗爆的方法 在Linux系統(tǒng)上,使用ps -aux|grep java可以...
    王廣帥閱讀 1,508評(píng)論 0 2
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,837評(píng)論 18 139
  • 微雨洗清秋,霾去霧亦收。 天高云送雁,呼朋踏山丘。 路長(zhǎng)天色晚,興盡客不留。 醉臥詩(shī)書上,一夢(mèng)到白頭。
    茶當(dāng)酒閱讀 192評(píng)論 4 10
  • 起初,給小朵報(bào)名最美童聲比賽只是想著讓她玩一下,鍛煉鍛煉膽量。 從報(bào)名到初賽到進(jìn)入半決賽,她經(jīng)歷了許多… 從剛開始...
    Dora的念念叨叨閱讀 1,258評(píng)論 2 1