最近代码中常用的System.exit(),就来看看源码。

首先位于java.lang.System中,源码如下:

  1. /**
  2. * Terminates the currently running Java Virtual Machine. The
  3. * argument serves as a status code; by convention, a nonzero status
  4. * code indicates abnormal termination.
    用来终止当前正在运行的JVM。参数用作状态码;根据惯例:非零状态码表示非正常终止
  1. * <p>
  2. * This method calls the <code>exit</code> method in class
  3. * <code>Runtime</code>. This method never returns normally.
    该方法调用Runtime类的exit方法,该方法永远不会正常返回
  1. * <p>
  2. * The call <code>System.exit(n)</code> is effectively equivalent to
  3. * the call:
  4. * <blockquote><pre>
  5. * Runtime.getRuntime().exit(n)
  6. * </pre></blockquote>
  7. *
  8. * @param status exit status.
  9. * @throws SecurityException
  10. * if a security manager exists and its <code>checkExit</code>
  11. * method doesn't allow exit with the specified status.
  12. * @see java.lang.Runtime#exit(int)
  13. */
  14. public static void exit(int status) {
  15. Runtime.getRuntime().exit(status);
  16. }

  是用来终止JVM的,也就是说整个程序都停止了,占用的内存也释放了。

继续往下找Runtime.getRuntime():返回与当前java应用程序相关的运行时对象

  1. /**
  2. * Returns the runtime object associated with the current Java application.
  3. * Most of the methods of class <code>Runtime</code> are instance
  4. * methods and must be invoked with respect to the current runtime object.
  5. *
  6. * @return the <code>Runtime</code> object associated with the current
  7. * Java application.
  8. */
  9. public static Runtime getRuntime() {
  10. return currentRuntime;
  11. }

再来看Runtime类的exit方法:

  1. /**
  2. * Terminates the currently running Java virtual machine by initiating its
  3. * shutdown sequence. This method never returns normally. The argument
  4. * serves as a status code; by convention, a nonzero status code indicates
  5. * abnormal termination.
    通过启动虚拟机的关闭序列,终止当前正在运行的 Java 虚拟机。此方法从不正常返回。
    可以将变量作为一个状态码;根据惯例,非零的状态码表示非正常终止
  1. *
  2. * <p> The virtual machine's shutdown sequence consists of two phases. In
  3. * the first phase all registered {@link #addShutdownHook shutdown hooks},
  4. * if any, are started in some unspecified order and allowed to run
  5. * concurrently until they finish. In the second phase all uninvoked
  6. * finalizers are run if {@link #runFinalizersOnExit finalization-on-exit}
  7. * has been enabled. Once this is done the virtual machine {@link #halt
  8. * halts}.
    虚拟机的关闭序列包含两个阶段。
    在第一个阶段中,会以某种未指定的顺序启动所有已注册的关闭钩子 (hook)(如果有的话),
    并且允许它们同时运行直至结束。
    在第二个阶段中,如果已启用退出终结,则运行所有未调用的终结方法。一旦完成这个阶段,虚拟机就会暂停
  1. *
  2. * <p> If this method is invoked after the virtual machine has begun its
  3. * shutdown sequence then if shutdown hooks are being run this method will
  4. * block indefinitely. If shutdown hooks have already been run and on-exit
  5. * finalization has been enabled then this method halts the virtual machine
  6. * with the given status code if the status is nonzero; otherwise, it
  7. * blocks indefinitely.
    如果在虚拟机已开始其关闭序列后才调用此方法,那么若正在运行关闭钩子,则将无限期地阻断此方法。
    如果已经运行完关闭钩子,并且已启用退出终结 (on-exit finalization),那么此方法将利用给定的状态码
    (如果状态码是非零值)暂停虚拟机;否则将无限期地阻断虚拟机。
  1. *
  2. * <p> The <tt>{@link System#exit(int) System.exit}</tt> method is the
  3. * conventional and convenient means of invoking this method. <p>
  4. *
  5. * @param status
  6. * Termination status. By convention, a nonzero status code
  7. * indicates abnormal termination.
  8. *
  9. * @throws SecurityException
  10. * If a security manager is present and its <tt>{@link
  11. * SecurityManager#checkExit checkExit}</tt> method does not permit
  12. * exiting with the specified status
  13. *
  14. * @see java.lang.SecurityException
  15. * @see java.lang.SecurityManager#checkExit(int)
  16. * @see #addShutdownHook
  17. * @see #removeShutdownHook
  18. * @see #runFinalizersOnExit
  19. * @see #halt(int)
  20. */
  21. public void exit(int status) {
      
  22. SecurityManager security = System.getSecurityManager();
    // 如果已经为当前应用程序建立了安全管理器,则返回此安全管理器;否则,返回null。
  1.      if (security != null) {
  2. security.checkExit(status);
    // 如果不允许调用线程使用特定的状态码暂停java虚拟机,则抛出SecurityException
         }
         // 安全检查完毕,下来是正式终止,status的非0与0状态得到体现
    Shutdown.exit(status);
  1. }

接下来看看Shutdown.exit(status)的源码:在此区分0与非0

  1. /* Invoked by Runtime.exit, which does all the security checks.
  2. * Also invoked by handlers for system-provided termination events,
  3. * which should pass a nonzero status code.
  4. */
  5. static void exit(int status) {
  6. boolean runMoreFinalizers = false;
  7. synchronized (lock) {
  8. if (status != 0) runFinalizersOnExit = false;
  9. switch (state) {
  10. case RUNNING: /* Initiate shutdown */
  11. state = HOOKS;
  12. break;
  13. case HOOKS: /* Stall and halt */
  14. break;
  15. case FINALIZERS:
  16. if (status != 0) {
  17. /* Halt immediately on nonzero status */
  18. halt(status);
  19. } else {
  20. /* Compatibility with old behavior:
  21. * Run more finalizers and then halt
  22. */
  23. runMoreFinalizers = runFinalizersOnExit;
  24. }
  25. break;
  26. }
  27. }
  28. if (runMoreFinalizers) {
  29. runAllFinalizers();
  30. halt(status);
  31. }
  32. synchronized (Shutdown.class) {
  33. /* Synchronize on the class object, causing any other thread
  34. * that attempts to initiate shutdown to stall indefinitely
  35. */
  36. sequence();
  37. halt(status);
  38. }
  39. }

System.exit()源码分析的更多相关文章

  1. Integer面试连环炮以及源码分析

    场景:   昨天有位朋友去面试,我问他面试问了哪些问题,其中问了Integer相关的问题,以下就是面试官问的问题,还有一些是我对此做了扩展. 问:两个new Integer 128相等吗? 答:不.因 ...

  2. Integer面试连环炮以及源码分析(转)

    场景:   昨天有位朋友去面试,我问他面试问了哪些问题,其中问了Integer相关的问题,以下就是面试官问的问题,还有一些是我对此做了扩展. 问:两个new Integer 128相等吗? 答:不.因 ...

  3. 【精】EOS智能合约:system系统合约源码分析

    系统合约在链启动阶段就会被部署,是因为系统合约赋予了EOS链资源.命名拍卖.基础数据准备.生产者信息.投票等能力.本篇文章将会从源码角度详细研究system合约. 关键字:EOS,eosio.syst ...

  4. zookeeper源码分析之四服务端(单机)处理请求流程

    上文: zookeeper源码分析之一服务端启动过程 中,我们介绍了zookeeper服务器的启动过程,其中单机是ZookeeperServer启动,集群使用QuorumPeer启动,那么这次我们分析 ...

  5. 大数据之Oozie——源码分析(一)程序入口

    工作中发现在oozie中使用sqoop与在shell中直接调度sqoop性能上有很大的差异.为了更深入的探索其中的缘由,开始了oozie的源码分析之路.今天第一天阅读源码,由于没有编译成功,不能运行测 ...

  6. 【JUC】JDK1.8源码分析之AbstractQueuedSynchronizer(二)

    一.前言 在锁框架中,AbstractQueuedSynchronizer抽象类可以毫不夸张的说,占据着核心地位,它提供了一个基于FIFO队列,可以用于构建锁或者其他相关同步装置的基础框架.所以很有必 ...

  7. .net源码分析 - ConcurrentDictionary<TKey, TValue>

    List源码分析 Dictionary源码分析 ConcurrentDictionary源码分析 继上篇Dictionary源码分析,上篇讲过的在这里不会再重复 ConcurrentDictionar ...

  8. YARN DistributedShell源码分析与修改

    YARN DistributedShell源码分析与修改 YARN版本:2.6.0 转载请注明出处:http://www.cnblogs.com/BYRans/ 1 概述 2 YARN Distrib ...

  9. 《深入理解Spark:核心思想与源码分析》——SparkContext的初始化(叔篇)——TaskScheduler的启动

    <深入理解Spark:核心思想与源码分析>一书前言的内容请看链接<深入理解SPARK:核心思想与源码分析>一书正式出版上市 <深入理解Spark:核心思想与源码分析> ...

随机推荐

  1. C语言 全局变量、静态全局变量、局部变量、静态局部变量

    //test.c #include <stdio.h> extern int global_var; void test_global_var() { global_var++; prin ...

  2. 关于CSS中的定位使用子绝父相(子类绝对位置和父类相对位置)

    关于CSS中的定位使用子绝父相(子类绝对位置和父类相对位置) 欢迎转发,但是请填写原博客地址https://www.cnblogs.com/JNovice/p/9536910.html  前言:最近在 ...

  3. css基础重点内容总结

    一.目录引入 ./同级(当前) ../上级目录  ../../上上级目录 二.标签种类: 1.块级标签(block):独占一行,宽高可设: 2.行内块标签(inline-block):不独占一行,宽高 ...

  4. webservice接口的开发和调用

    一.WebService的开发手段 使用Java开发WebService时可以使用以下两种开发手段 1. 使用JDK开发(1.6及以上版本) 2.使用CXF框架开发(工作中) 二.使用JDK开发Web ...

  5. DB2数据库常用的函数

    1.value函数 语法value(表达式1,表达式2)value函数是用返回一个非空的值,当其第一个参数非空,直接返回该参数的值,如果第一个参数为空,则返回第一个参数的值. eg:表示如果T1.ID ...

  6. Oracle 12c 单实例安装

    准备工作 实验环境:Redhat 6.6   Oracle 12c 12.2.0.1 1.官网下载 https://www.oracle.com/technetwork/database/enterp ...

  7. JavaScript核心--Function

    什么是: 保存一段可重用的代码段的对象 何时: 只要一段代码可能反复使用时,都要封装为函数,反复调用函数 如何: 创建: 3种: 1. 直接量: function 函数名(参数列表){ 函数体; re ...

  8. 使用guava过期map

    最近需要将微信的accesstoken保存到缓存里面,防止重复请求微信接口获取token,造成token请求次数超标,其实随便一个缓存都可以轻松解决,但是现有的环境中没有redis,没有memcahe ...

  9. Java链接MySQL数据库的配置文件

    文件名:db.properties(随便) driver = com.mysql.jdbc.Driver  //MySQL数据库驱动名url = jdbc:mysql://localhost:3306 ...

  10. Bigger-Mai 养成计划,前端基础学习之HTML

    HTML 超文本标记语言(Hyper Text Markup Language) 1.一套规则,浏览器认识的规则. 2.开发者: 学习Html规则 开发后台程序: - 写Html文件(充当模板的作用) ...