system.exit(0)和system.exit(1)区别

本文深入探讨了Java虚拟机通过System.exit(int status)方法实现的异常退出机制,通过自定义线程实例展示了其在实际场景中的应用。文章详细解释了状态参数status的作用,以及在不同状态下的退出行为。并通过一个具体的代码实例,直观展示了如何在main方法中启动自定义线程并使用System.exit(0)方法使整个程序退出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、查看java.lang.System的源代码,我们可以找到System.exit(status)这个方法的说明,代码如下:

/**
     * 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虚拟机, 如果status==0,表示jvm正常退出,如果status!=0非零,表示jvm非正常退出。


关于System.exit(int status)方法
System.exit(int status);//这个语句的功能是结束当前运行的Java虚拟机,其中的参数status是状态代码,当status不为0时表示这次结束Java虚拟机是一个不正常的结束,这个数是返回给操作系统的。一般在Windows底下,不正常退出状态码为-1,这里可写为System.exit(-1);
System.exit(int status)方法效果等同于于Runtime.getRuntime().exit(int n)方法

特殊案例分析:在main方法中,启动一个自定义线程,并执行system.exit方法

自定义线程代码:

package com.java.demo;

public class Calculator implements Runnable {
	private int number;

	public Calculator(int number) {

		this.number = number;
	}

	@Override
	public void run() {
		for (int i = 1; i <= 10; i++) {
			System.out.printf("%s: %d * %d = %d\n", Thread.currentThread().getName(), number, i, i * number);

		}

	}

}
测试方法:

package com.java.demo;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		
			Calculator calculator = new Calculator(1);
			Thread thread = new Thread(calculator);
			thread.start();	   //启动自定义线程
			
			System.out.println("hello world-----1");
			
			System.exit(0);  //执行jvm 退出
			
			System.out.println("hello world------2");
			

		
		
		
		
		

	}

}

结果展示:



结果分析:我们在main方法启动一个自定义的线程,但是控制台只是输出了一个字符串“hello word--------1”.但是自定义线程中的run()方法输出的内容没有打印。以下是我根据程序执行结果,描绘的程序执行图:



留个思考问题:如果在线程销毁方法中调用jvm退出方法,那我们的执行结果会发生怎么样的改变?




### 使用BroadcastReceiver与System.exit(0)实现应用关闭或处理特定广播事件 在 Android 中,`BroadcastReceiver` 是用于响应来自系统或其他应用程序的广播消息的一种机制。通过 `BroadcastReceiver` 可以监听各种类型的广播事件并执行相应的操作。如果需要结合 `System.exit(0)` 来实现程序退出或者处理某些特定广播事件,则可以按照以下方式设计。 #### 广播接收器的基础设置 创建一个继承自 `BroadcastReceiver` 的类,并重写其 `onReceive()` 方法,在该方法中定义接收到广播后的具体行为[^3]。例如: ```java public class ExitAppReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if ("com.example.EXIT_APP".equals(intent.getAction())) { System.exit(0); } } } ``` 上述代码片段展示了当接收到指定动作 `"com.example.EXIT_APP"` 的广播时,会触发 `System.exit(0)` 关闭整个 JVM 实例的行为。 #### 动态注册广播接收器 可以通过 Java/Kotlin 代码动态注册广播接收器来捕获特定意图(Intent)。以下是动态注册的一个例子: ```java // 创建广播接收器实例 ExitAppReceiver exitAppReceiver = new ExitAppReceiver(); // 定义过滤条件 IntentFilter filter = new IntentFilter(); filter.addAction("com.example.EXIT_APP"); // 注册广播接收器 registerReceiver(exitAppReceiver, filter); ``` 此部分实现了将 `ExitAppReceiver` 对象绑定到当前上下文中,并指定了它所关注的动作名称为 `"com.example.EXIT_APP"`[^1]。 #### 静态声明广播接收器 另一种更持久化的方式是在 `AndroidManifest.xml` 文件里静态配置广播接收器及其感兴趣的意图过滤规则。如下所示: ```xml <receiver android:name=".ExitAppReceiver"> <intent-filter> <action android:name="com.example.EXIT_APP"/> </intent-filter> </receiver> ``` 这种方式使得即使应用未运行也能对外部发送的相关广播做出反应[^2]。 需要注意的是,调用 `System.exit(0)` 虽然能够强制终止虚拟机进程从而达到结束应用的效果,但这并不是推荐的做法,因为这可能会绕过正常的组件销毁流程而导致资源泄漏等问题。通常建议采用标准 API 如 `finishAffinity()`, 或者通知 Activity/Service 自己去完成清理工作后再自行退出。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值