System.exit()源码分析

  最近代码中经常使用的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); } }
相关文章
相关标签/搜索