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. 【转】C# Linq 交集、并集、差集、去重

    using System.Linq; List<string> ListA = new List<string>(); List<string> ListB = n ...

  2. nodejs框架express实现登录

    目录: 访问视图 Post请求 Post请求 - body(1) Post请求 - body(2) Post登陆1 Post登陆2 页面访问控制1 页面访问控制2 访问视图 前面我们已经添加了视图模板 ...

  3. 【svn】一个设置,少写几个字

    以下场景仅适用于修改bug的时候,在提交代码的时候少写几个字,嘿嘿: 1.打开[SVN 属性],在代码目录右键 2.打开BUG跟踪设置窗口 3.输入BUG的URL前缀以及%BUGID%,如 复选框,建 ...

  4. 配置文件之SharedPreferences

    新建配置文件类 /** * Created by RongGuang * 应用程序配置信息 */ public class AppOption { public static final String ...

  5. redmine设置

    接上篇. 1.redmine新版本已经比较强大了,可以定制所有字段(含标准字段和自定义字段)的读写属性.这里为了避免字段过多影响用户感受,希望增加功能将不相关的字段屏蔽,下载插件Workflow Hi ...

  6. Linux 多线程编程 实例 2

    编写一个程序,开启3个线程,这3个线程的ID分别为A.B.C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示:如:ABCABC….依次递推. 使用条件变量来实现: #inc ...

  7. CSS 笔记三(Tables/Box Model/Outline)

    CSS Tables border border: border-width border-style border-color|initial|inherit; border-width borde ...

  8. WPF:常见问题

    1.自定义Main函数 背景: wpf 默认的Main函数在 App.g.cs文件中,在App.xmal.cs内自定义Main函数后冲突. 解决方法: 法一: 1)新建class1.cs类,在其中设置 ...

  9. win7下JDK环境变量设置方法

    来源于:http://jingyan.baidu.com/article/e9fb46e1b2b3347521f766c1.html

  10. c++中endl的函义

    c++中endl的函义是回车的函义,Enter