参考链接: Java中的System.exit() 1、查看java.lang.System的源代码,我们可以找到System.exit(status)这个方法的说明,代码如下: /** * *
* The call System.exit(n) is effectively equivalent to
* the call: 关于System.exit(int status)方法
System.exit(int status);//这个语句的功能是结束当前运行的Java虚拟机,其中的参数status是状态代码,当status 一般在Windows底下,不正常退出状态码为-1,这里可写为System.exit(-1);
System.exit(int status)方法效果等同于于Runtime.getRuntime().exit (int n)方法
特殊案例分析:在main方法中,启动一个自定义线程,并执行system.exit方法
自定义线程代码:
package com.java.demo;
public class
System.exit(0)是将你的整个虚拟机里的内容都停掉了 ,而dispose()只是关闭这个窗口,但是并没有停止整个application exit() 。无论如何,内存都释放了! 也就是说连JVM都关闭了,内存里根本不可能还有什么东西 System.exit(0)是正常退出程序,而System.exit(1)或者说非0表示非正常退出程序 System.exit(status)不管
The call System.exit(n) is effectively equivalent to the call: Runtime.getRuntime().exit(n) Parameters See Also: Runtime.exit(int) System.exit(int status) 的作用是: ? 直译:终止当前正在运行的Java虚拟机。 *
* The call System.exit(n) is effectively equivalent to
* the call: 关于示例就说到这里吧,其实在实际开发中经常遇到,比如大数据开发当中, MapReduce 中使用System.exit(job.waitForCompletion(true)?
= 2) { System.err.println("Usage: wordcount <in> <out>"); System.exit(2); } (job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit = 2) { System.err.println("Usage: wordcount <in> <out>"); System.exit(2); } Job (job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit = 2) { System.err.println("Usage: wordcount <in> <out>"); System.exit(2); } conf.set
1.典型回答正常运行的情况下,finally 中的代码是一定会执行的,但是,如果遇到以下异常情况,那么 finally 中的代码就不会继续执行了:程序在 try 块中遇到 System.exit() 方法 与 System.exit()方法不同,此方法不会触发 JVM 关闭序列。因此,当我们调用 halt 方法时,都不会执行关闭钩子或终结器。 3.知识扩展System.exit() 和 Runtime.getRuntime().halt() 都可以用于终止 Java 程序的执行,但它们之间有以下区别:System.exit():来自 Java.lang.System ; // 使用 System.exit() 退出程序 System.exit(0); } finally { System.out.println ; } }}以上程序的执行结果如下: 小结正常运行的情况下,finally 中的代码是一定会执行的,但是,如果遇到 System.exit() 方法或 Runtime.getRuntime
return controller; } catch (Throwable e) { e.printStackTrace(); System.exit buildCommandlineOptions(options), new PosixParser()); if (null == commandLine) { System.exit , namesrvConfig); MixAll.printObjectProperties(console, nettyServerConfig); System.exit initResult) { controller.shutdown(); System.exit(-3); } //添加钩子函数 initResult) { controller.shutdown(); System.exit(-3); }
1.典型回答 正常运行的情况下,finally 中的代码是一定会执行的,但是,如果遇到以下异常情况,那么 finally 中的代码就不会继续执行了: 程序在 try 块中遇到 System.exit() 与 System.exit()方法不同,此方法不会触发 JVM 关闭序列。因此,当我们调用 halt 方法时,都不会执行关闭钩子或终结器。 2.考点分析 正常运行的情况下,finally 中的代码是一定会执行的,但是,如果遇到 System.exit() 方法或 Runtime.getRuntime().halt() 方法,或者是 try 3.知识扩展 System.exit() 和 Runtime.getRuntime().halt() 都可以用于终止 Java 程序的执行,但它们之间有以下区别: System.exit():来自 Java.lang.System ; // 使用 System.exit() 退出程序 System.exit(0); } finally { System.out.println
解决方案 找到退出程序代码的地方: 把System.exit(0)屏蔽以后 程序再次退出 。就不会出现问题了。 ; exitTime = System.currentTimeMillis(); } else { finish(); // System.exit (0); //用了这行代码退出App会黑屏 } } System.exit(0)表示正常退出JVM,而System.exit(1)表示异常退出JVM。 Android并不是调用System.exit来退出应用的。调用System.exit(0); 会黑一下屏幕是因为当前这个Activity重新onCreate了一次。
`System.exit()` 导致 JVM 终止 2. 无限循环或死锁 3. 程序被强制终止 示例代码演示 示例 1:正常情况下执行 finally 块 示例 2:发生异常时执行 finally 块 示例 3:System.exit() 导致 JVM 终止 示例 4:无限循环导致 System.exit() 导致 JVM 终止 在Java中,如果在try或catch块中调用了System.exit(int status)方法,它将导致JVM终止,即Java虚拟机将立即退出。 以下是一个示例: try { // 一些操作 System.exit(0); // JVM 将终止 } finally { // 这里的代码不会执行 } 2. 大多数情况下,finally块中的代码都会执行,但在某些特殊情况下,如调用System.exit()、无限循环或程序被强制终止时,finally块中的代码可能不会执行。
而且,基于平台通用性的考虑,更推荐应用程序使用System.exit(0)这种方式退出JVM。 5.不能在钩子调用System.exit(),否则卡住JVM的关闭过程,但是可以调用Runtime.halt()。 显然,我们应该正常关闭JVM(异常关闭JVM的情形不希望发生,也无法百分之百地完全杜绝),即执行:System.exit(),Ctrl + C, kill -15 进程ID。 System.exit():通常我们在程序运行完毕之后调用,这是在应用代码中写死的,无法在进程外部进行调用。 (0)正常关闭JVM,触发关闭钩子执行收尾工作 System.exit(0); } }); 第二步:注册关闭钩子 Runtime.getRuntime().addShutdownHook
intent.putExtra("exit", true); context.startActivity(intent); // 结束进程 // System.exit 结束进程 // System.exit(0); 优点 使用简单、方便 缺点 使用范围局限:只能结束当前任务栈的Activity,若出现多任务栈(即采用SingleInstance (0);结束进程 // 原因:发送广播这个方法之后,不会等到广播接收器收到广播,程序就开始执行下一句System.exit(0),然后就直接变成执行System.exit(0)的效果了。 () // System.exit() = Java中结束进程的方法:关闭当前JVM虚拟机 System.exit(0); // System.exit(0)和System.exit(1)的区别 System.exit(0):正常退出; // 2. System.exit(1):非正常退出,通常这种退出方式应该放在catch块中。
结束进程 // System.exit(0); 优点 使用简单、方便 缺点 使用范围局限:只能结束当前任务栈的Activity,若出现多任务栈(即采用SingleInstance (0);结束进程 // 原因:发送广播这个方法之后,不会等到广播接收器收到广播,程序就开始执行下一句System.exit(0),然后就直接变成执行System.exit(0)的效果了。 : activityLinkedList) { activity.finish(); } // 结束进程 // System.exit () // System.exit() = Java中结束进程的方法:关闭当前JVM虚拟机 System.exit(0); // System.exit(0)和System.exit(1)的区别 System.exit(0):正常退出; // 2. System.exit(1):非正常退出,通常这种退出方式应该放在catch块中。
System.out.println(br.readLine()); br.close(); } catch (IOException e) { System.exit * * The call System.exit(n) is effectively equivalent to * the call: * * Runtime.getRuntime * * The {@link System#exit(int) System.exit} method is the * conventional and convenient System.out.println(br.readLine()); br.close(); } catch (IOException e) { System.exit 因此如果我们可以修改 SecurityManager 如果检查退出时抛出异常,那么在 执行 System.exit(2) 时就会发生异常,最终依然会执行到 finally代码块。
还可能一种可能就是,执行完主线程后就直接System.exit() 退出jvm。 下面我们分析下源码,看看到底是什么情况 Debug模式下运行 testSleep() 方法,如下: ? testRunServer.run(); } catch (Throwable JavaDoc e) { e.printStackTrace(); // don't allow System.exit (0) to swallow exceptions } finally { // fix for 14434 System.exit(0); } } 从代码中我们发现 ,当执行完testSleep()方法的主线程时,就会调用 finally里面的 System.exit(0) 方法,让JVM强制退出。
结束进程 // System.exit(0); 优点 1. ); } 步骤3:当需要退出App时 发送广播请求 context.sendBroadcast(new Intent(BaseApplication.EXIT)); // 注:此处不能使用:System.exit (0);结束进程 // 原因:发送广播这个方法之后,不会等到广播接收器收到广播,程序就开始执行下一句System.exit(0),然后就直接变成执行System.exit(0)的效果了。 () // System.exit() = Java中结束进程的方法:关闭当前JVM虚拟机 System.exit(0); // System.exit(0)和System.exit(1)的区别 System.exit(0):正常退出; // 2. System.exit(1):非正常退出,通常这种退出方式应该放在catch块中。
); }catch(FileNotFoundException e) { System.out.println("找不到指定文件"); System.exit num + "个字节"); }catch(IOException e1) { System.out.println("文件读取错误"); System.exit ; }catch(FileNotFoundException e2) { System.out.println("找不到指定文件"); System.exit (-1); }catch(IOException e1) { System.out.println("文件复制错误"); System.exit
= 2) { System.out.println("命令执行错误"); System.exit(1);// 退出程序 } File inFile = new File(str[0] inFile.exists()) { //源文件是否存在 System.out.println("源文件不存在"); System.exit(1); } File outFile = = 2) { System.out.println("命令执行错误"); System.exit(1);// 退出程序 } File inFile = new File(str[0] inFile.exists()) { //源文件是否存在 System.out.println("源文件不存在"); System.exit(1); } File outFile =
System.out.println(br.readLine()); br.close(); } catch (IOException e) { System.exit System.out.println(br.readLine()); br.close(); } catch (IOException e) { System.exit 因此如果我们可以修改 SecurityManager 如果检查退出时抛出异常,那么在执行System.exit(2) 时就会发生异常,最终依然会执行到 finally代码块。 System.out.println(br.readLine()); br.close(); } catch (IOException e) { System.exit
Context.ACTIVITY_SERVICE); activityMgr.restartPackage(context.getPackageName()); System.exit \"></uses-permission> 4. android.os.Process.killProcess(android.os.Process.myPid()) //获取PID System.exit (0); 缺点: 如果是在第一个 Activity 调用 Process.killProcess 或 System.exit(0) 都会 kill 掉当前进程。 但是如果不是在第一个 Activity 中调用,如 ActivityA 启动 ActivityB ,你在 ActivityB 中调用 Process.killProcess 或 System.exit
执行时机: ShutdownHook在以下情况下会被执行: 程序正常退出(调用System.exit(int status)方法) 用户按下Ctrl+C终止程序 操作系统关闭 JVM崩溃 执行顺序 (-1); } } 当程序执行到System.exit(int status)或者不写System.exit(int status)手工按下Ctrl+C终止程序时,注册的ShutdownHook 避免在ShutdownHook中调用System.exit():如果在ShutdownHook中调用System.exit(),会导致当前的JVM进程卡住,无法正常退出。 这是因为System.exit()会触发终结器(Terminator)进程,这是一个操作系统级别的操作,它会等待当前进程中的所有线程都结束之后,才会释放资源并退出进程。 因此,如果在ShutdownHook中调用System.exit(),会导致资源无法正确释放,从而可能引发资源泄漏。