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)確一些。
- 調(diào)用方法后,線程會(huì)退出。
- 未捕獲的異常被線程拋出,但如果有其他非守護(hù)線程,程序?qū)⒗^續(xù)運(yùn)行。
- 反饋狀態(tài)碼,一般在腳本中有用。
- 線程退出,還是做一些清理動(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);
}