最近代碼中經常使用的System.exit(),就來看看源碼。java
首先位於java.lang.System中,源碼以下:安全
/** * Terminates the currently running Java Virtual Machine. The * argument serves as a status code; by convention, a nonzero status * code indicates abnormal termination.
用來終止當前正在運行的JVM。參數用做狀態碼;根據慣例:非零狀態碼錶示非正常終止
* <p> * This method calls the <code>exit</code> method in class * <code>Runtime</code>. This method never returns normally.
該方法調用Runtime類的exit方法,該方法永遠不會正常返回
* <p> * The call <code>System.exit(n)</code> is effectively equivalent to * the call: * <blockquote><pre> * Runtime.getRuntime().exit(n) * </pre></blockquote> * * @param status exit status. * @throws SecurityException * if a security manager exists and its <code>checkExit</code> * method doesn't allow exit with the specified status. * @see java.lang.Runtime#exit(int) */ public static void exit(int status) { Runtime.getRuntime().exit(status); }
是用來終止JVM的,也就是說整個程序都中止了,佔用的內存也釋放了。app
繼續往下找Runtime.getRuntime():返回與當前java應用程序相關的運行時對象ide
/** * Returns the runtime object associated with the current Java application. * Most of the methods of class <code>Runtime</code> are instance * methods and must be invoked with respect to the current runtime object. * * @return the <code>Runtime</code> object associated with the current * Java application. */ public static Runtime getRuntime() { return currentRuntime; }
再來看Runtime類的exit方法:ui
/** * 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.
經過啓動虛擬機的關閉序列,終止當前正在運行的 Java 虛擬機。此方法從不正常返回。
能夠將變量做爲一個狀態碼;根據慣例,非零的狀態碼錶示非正常終止
* * <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}.
虛擬機的關閉序列包含兩個階段。
在第一個階段中,會以某種未指定的順序啓動全部已註冊的關閉鉤子 (hook)(若是有的話),
而且容許它們同時運行直至結束。
在第二個階段中,若是已啓用退出終結,則運行全部未調用的終結方法。一旦完成這個階段,虛擬機就會暫停
* * <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.
若是在虛擬機已開始其關閉序列後才調用此方法,那麼若正在運行關閉鉤子,則將無限期地阻斷此方法。
若是已經運行完關閉鉤子,而且已啓用退出終結 (on-exit finalization),那麼此方法將利用給定的狀態碼
(若是狀態碼是非零值)暫停虛擬機;不然將無限期地阻斷虛擬機。
* * <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) {
SecurityManager security = System.getSecurityManager();
// 若是已經爲當前應用程序創建了安全管理器,則返回此安全管理器;不然,返回null。
if (security != null) { security.checkExit(status);
// 若是不容許調用線程使用特定的狀態碼暫停java虛擬機,則拋出SecurityException
}
// 安全檢查完畢,下來是正式終止,status的非0與0狀態獲得體現
Shutdown.exit(status);
}
接下來看看Shutdown.exit(status)的源碼:在此區分0與非0this
/* Invoked by Runtime.exit, which does all the security checks. * Also invoked by handlers for system-provided termination events, * which should pass a nonzero status code. */ static void exit(int status) { boolean runMoreFinalizers = false; synchronized (lock) { if (status != 0) runFinalizersOnExit = false; switch (state) { case RUNNING: /* Initiate shutdown */ state = HOOKS; break; case HOOKS: /* Stall and halt */ break; case FINALIZERS: if (status != 0) { /* Halt immediately on nonzero status */ halt(status); } else { /* Compatibility with old behavior: * Run more finalizers and then halt */ runMoreFinalizers = runFinalizersOnExit; } break; } } if (runMoreFinalizers) { runAllFinalizers(); halt(status); } synchronized (Shutdown.class) { /* Synchronize on the class object, causing any other thread * that attempts to initiate shutdown to stall indefinitely */ sequence(); halt(status); } }