Java的System.exit()详解

说到 java.lang.System.exit() 这个方法,咱们先看下 JDK8 的 API 中怎么说
在这里插入图片描述
为方便大家分析,我给粘下来

public static void exit(int status)
Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.
This method calls the exit method in class Runtime. This method never returns normally.

The call System.exit(n) is effectively equivalent to the call:

 Runtime.getRuntime().exit(n)
 
Parameters:
status - exit status.
Throws:
SecurityException - if a security manager exists and its checkExit method doesn't allow exit with the specified status.
See Also:
Runtime.exit(int)

System.exit(int status) 的作用是:
在这里插入图片描述
直译:终止当前正在运行的Java虚拟机。参数用作状态代码;按照惯例,非零状态代码表示异常终止。此方法在类运行时调用exit方法。此方法从不正常返回。

再看下源码注释

    /**
     * Terminates the currently running Java Virtual Machine. The
     * argument serves as a status code; by convention, a nonzero status
     * code indicates abnormal termination.
     * <p>
     * This method calls the <code>exit</code> method in class
     * <code>Runtime</code>. This method never returns normally.
     * <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);
    }

也就是说,该方法用来结束当前正在运行中的java虚拟机。

public static void exit(int status)

status 为 0:表示正常退出程序,也就是结束当前正在运行中的java虚拟机。
status 为 1 或 -1 或 任何其他非零值 :表示非正常退出当前程序。

注意:此方法不会返回任何值。

正常退出和非正常退出具体是什么个情况?

答:正常退出 是指如果当前程序还有在执行的任务,则等待所有任务执行完成以后再退出;非正常退出 是只要时间到了,立刻停止程序运行,不管是否还有任务在执行。这个效果就类似于 linux 里面的 kill -9kill -15

用法示例

下面看个示例就可以搞清楚了。
在这里插入图片描述
关于示例就说到这里吧,其实在实际开发中经常遇到,比如大数据开发当中, MapReduce 中使用System.exit(job.waitForCompletion(true)?0:1);可以判短程序是否正常退出。

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
System.exitJava中的一个方法,用于终止当前正在运行的Java虚拟机。根据传入的参数不同,System.exit可以实现正常终止和异常终止两种状态的退出。当参数为0时,即System.exit(0),表示程序正常退出。而当参数为非零的整数时,即System.exit(非零的int型参数),表示程序异常退出。\[1\]\[2\] 正常终止和异常终止的区别在于使用场景的不同。正常终止一般用于程序按照预期执行完毕后需要停止程序的情况,而异常终止一般用于捕获到异常后需要立即停止程序的情况。例如,在一个if-else判断中,如果程序按照预期执行到最后需要停止程序,可以使用System.exit(0);而如果捕获到异常后需要停止程序,可以使用System.exit(1)。\[1\] 举个例子,假设我们有一个Java类名为test,其中使用了try-catch语句。在try块中进行除法运算,如果输入的除数为0,则会抛出ArithmeticException异常,并执行catch块中的代码。在catch块中,可以输出异常信息并使用System.exit(-1)来异常终止程序。\[2\]\[3\] 总之,System.exit方法可以用于终止当前正在运行的Java虚拟机,根据传入的参数不同可以实现正常终止和异常终止两种状态的退出。具体使用哪种状态的退出取决于使用场景的需求。 #### 引用[.reference_title] - *1* [java终止程序语句总结 System.exit(1)、System.exit(0)、return;break;continue;](https://blog.csdn.net/mingtiandejueze/article/details/83388043)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [System.exit()方法的详解](https://blog.csdn.net/qq_40474507/article/details/119116506)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值