如何在几秒钟后退出Java程序 [英] How to make Java program exit after a couple of seconds
本文介绍了如何在几秒钟后退出Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
无论如何,我可以在几秒钟之后退出java程序,例如5秒。
Is there anyway I can exit a java program after a couple of seconds e.g. 5 seconds.
我知道你可以退出java程序:
I know you can quit the java program using:
System.exit(0);
但我不确定0代表自此代码后的秒数:
But I'm not sure whether the 0 stands for seconds since this code:
System.exit(10);
也会立即退出
推荐答案
System.exit(0)指定程序的退出错误代码。
System.exit(0) specifies the exit error code of the program.
你可以把它放在计时器上并安排任务
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));
}
}
然后你可以调用TimedExit ()
and then you can just called TimedExit()
这篇关于如何在几秒钟后退出Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文