如何让 Java 程序在几秒钟后退出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15747277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:43:33  来源:igfitidea点击:

How to make Java program exit after a couple of seconds

javasystemexitseconds

提问by EmaadP

Is there anyway I can exit a java program after a couple of seconds e.g. 5 seconds.

无论如何我可以在几秒钟后退出一个java程序,例如5秒。

I know you can quit the java program using:

我知道您可以使用以下方法退出 java 程序:

System.exit(0);

But I'm not sure whether the 0 stands for seconds since this code:

但我不确定 0 是否代表自这段代码以来的秒数:

System.exit(10);

also exits instantly

也立即退出

回答by nate_weldon

System.exit(0) specifies the exit error code of the program.

System.exit(0) 指定程序的退出错误代码。

you can put it on a timer and schedule the task

你可以把它放在计时器上并安排任务

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TimedExit {
Timer timer = new Timer();
TimerTask exitApp = new TimerTask() {
public void run() {
    System.exit(0);
    }
};

public TimedExit() {
timer.schedule(exitApp, new Date(System.currentTimeMillis()+5*1000));
    }

}

and then you can just called TimedExit()

然后你就可以调用 TimedExit()

回答by Juvanis

You can invoke Thread.sleep()just before you exit your program:

您可以Thread.sleep()在退出程序之前调用:

// Your code goes here.

try 
{
   Thread.sleep(5000);
} 
catch (InterruptedException e) 
{
   // log the exception.
}

System.exit(0);

回答by Francisco Spaeth

From System.exit(int)method documentation:

来自System.exit(int)方法文档:

Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

终止当前运行的 Java 虚拟机。参数用作状态码;按照惯例,非零状态代码表示异常终止。

If you need to execute something during the time waiting for exit, you could create a control thread, that will just wait for the right time to perform the exit like this:

如果您需要在等待退出期间执行某些操作,您可以创建一个控制线程,它只会等待合适的时间来执行退出,如下所示:

public class ExitFewMiliseconds {

    public static void main(String args[]) {

        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.exit(0);
            }
        }).start();

        while (true) {
            System.out.println("I'm doing something");
        }
    }

}

If nothing shall be executing while waiting for exit, you could simply use a Thread.sleep(ms)

如果在等待退出时什么都不执行,您可以简单地使用Thread.sleep(ms)

回答by Avi

The 0 you pass into System.exit(0) has nothing to do with how long it will wait before exiting. Javadocs are your friend. From the javadoc:

您传递给 System.exit(0) 的 0 与退出前等待的时间无关。 Javadocs 是您的朋友。从javadoc:

The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

参数用作状态码;按照惯例,非零状态代码表示异常终止。

Other answers already cover how to wait 5 seconds before exiting if you really need to do that.

如果您确实需要这样做,其他答案已经涵盖了如何在退出前等待 5 秒。

回答by tarunajain

import java.util.Timer;
import java.util.TimerTask;

/**
 * Simple demo that uses java.util.Timer to schedule a task 
 * to execute once 5 seconds have passed.
 */
class Reminder {

    Timer timer;

    public Reminder(int seconds) {
        timer = new Timer();
        timer.schedule(new RemindTask(), seconds * 1000);
    }

    class RemindTask extends TimerTask {
        public void run() {
            System.out.format("Time's up!%n");
            System.exit();
            timer.cancel(); //Terminate the timer thread
        }
    }

    public static void main(String args[]) {
        new Reminder(10);
        System.out.format("Task scheduled.%n");
    }
}

In this way you can use the Timer class and exit the system after a particular time interval

通过这种方式,您可以使用 Timer 类并在特定时间间隔后退出系统