System.exit(0)作用

 

public class HelloGoodbye{

try{

System.out.println(“Hello World”);

System.exit(0);

} finally {

System.out.println(“Goodbye World”);

}}

上面这段代码会打印出什么呢?要知道答案需要了解System.exit(0)这个方法在执行过程中到底发生了哪些事情。API文档上描述System.exit方法会导致当前运行线程停止并使其它线程都终止,因此上面这段代码中的finally代码块不会被执行。

进一步探索System.exit被调用后的行为,它在虚拟机在退出前会执行两个清除任务。第一,它会执行所有通过Runtime.addShutdownHook注册的shutdown hooks.它能有效的释放JVM之外的资源。第二,执行清除任务,运行相关的finalizer方法终结对象。

如果需要在调用System.exit方法后仍然打印出“Goodbye World”,可采用如下的方法:

System.out.println(“Hello World”);

Runtime.getRuntime().addShutdownHook(

new Thread(){

public void run(){

System.out.println(“Goodbye world”);

}

}

)

System.exit(0);

System.exit(0)表示正常退出JVM,而System.exit(1)表示异常退出JVM。参数只是通知操作系统程序的退出状态,0为正常,非0为异常。

System.exit(0)作用的更多相关文章

  1. android Activity类中的finish()、onDestory()和System.exit(0) 三者的区别

    android Activity类中的finish().onDestory()和System.exit(0) 三者的区别 Activity.finish() Call this when your a ...

  2. System.exit(0)和System.exit(1)区别

    System.exit(0)是将你的整个虚拟机里的内容都停掉了 ,而dispose()只是关闭这个窗口,但是并没有停止整个application exit() .无论如何,内存都释放了!也就是说连JV ...

  3. android开发时,finish()跟System.exit(0)的区别

      这两天在弄Android,遇到一个问题:所开发的小游戏中有背景音乐,玩的过程中始终有音乐在放着,然后在我退出游戏后,音乐还在播放! 我看了一下我最开始写的退出游戏的代码,就是简单的finish() ...

  4. System.exit(0)和System.exit(1)区别:

    System.exit(0)是将你的整个虚拟机里的内容都停掉了,而finish()只是退出了activity,并没有退出应用,Application还是存在于内存中的,除非被系统回收.无论如何,内存都 ...

  5. How to use Android Activity's finish(), onDestory() and System.exit(0) methods

    Activity.finish() Calling this method will let the system know that the programmer wants the current ...

  6. System.exit(0)

    表示程序正常退出 System.exit(status) 当status非0时,表示程序为非正常退出. status=0, 关闭当前正在运行的虚拟机. 求质因数分解的程序如下: 两种算法: 一种是用S ...

  7. android Process.killProcess 和 System.exit(0) 区别

    1 Process.killProcess  和 System.exit(0) 两个都会 kill 掉当前进程. 你可以打开 DDMS 查看进程号,或 adb shell 进入 shell 然后 ps ...

  8. system.exit(0) vs system.exit(1)

    2.解析 查看java.lang.System的源代码,我们可以找到System.exit(status)这个方法的说明,代码如下: /** * Terminates the currently ru ...

  9. android中 System.exit(0)的理解

    public class HelloGoodbye{ try{ System.out.println(“Hello World”); System.exit(0); } finally { Syste ...

随机推荐

  1. windows系统调用 互斥体mutex

    #include "iostream" #include "windows.h" using namespace std; class CCountUpDown ...

  2. jsp:中文乱码解决

    说明:request乱码指的是:浏览器向服务器发送的请求参数中包含中文字符,服务器获取到的请求参数的值是乱码: response乱码指的是:服务器向浏览器发送的数据包含中文字符,浏览器中显示的是乱码: ...

  3. WPF关于Generic.xaml

    如果需要用到Themes/Generic.xaml作为默认风格资源文件,不要忘了该项目的AssemblyInfo.cs中必须要有以下这段: [assembly: ThemeInfo( Resource ...

  4. jquery easyui DataGrid 数据表格 属性

    用法 1.  <table id="tt"></table> 1.  $('#tt').datagrid({ 2.      url:'datagrid_d ...

  5. transform scale 背景图片模糊怎么办?

    transform: translateZ(0) scale(1, 1); 就是这样(摊手表情),不晓得什么原理.

  6. springmvc 组合注解

    组合注解的意思就是一个注解中包含多个注解.在springmvc 的@RestController中,你就可发现. @Target(ElementType.TYPE) @Retention(Retent ...

  7. PowerDesigner反向工程,根据Oracle数据库结构生成ER图(2014-3-25记)

    01.添加数据服务端 02. PowerDesigner 1. 新建PDM:选择菜单File->New Model,[Model type]选择Physical Data Model,[DBMS ...

  8. Hadoop之为何不使用RAID?

    一.引言: 在一次和同事的讨论中遇到一个这样的问题:有一个hadoop集群,在hbase的put数据出现瓶颈,他们想要把datanode上的磁盘做成RAID 0(比如10块磁盘做成一个RAID 0), ...

  9. maven+Jenkins学习小记

    jenkins配置方法1,tomcat下载,解压,切换到bin目录,配置环境变量,地址为catalina.bat文件夹下,也就是bin目录,再配置path变量2,启动tomcat,dos命令,cata ...

  10. Web Performance Test: 如果使用Plugin过滤Dependent Request

    前言 由于Visual Studio的Web Performance Test是基于XML脚本的,留给用户修改测试行为的自由度并不高.因此,Plugin机制就对于实现很多客户化的配置显得很重要. 问题 ...