Is there anyway I can exit a java program after a couple of seconds e.g. 5 seconds.
I know you can quit the java program using:
System.exit(0);
But I'm not sure whether the 0 stands for seconds since this code:
System.exit(10);
also exits instantly
System.exit(0) specifies the exit error code of the program.
you can put it on a timer and schedule the task
and then you can just called TimedExit()
In this way you can use the Timer class and exit the system after a particular time interval
From System.exit(int) method documentation:
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:
If nothing shall be executing while waiting for exit, you could simply use a Thread.sleep(ms)
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:
Other answers already cover how to wait 5 seconds before exiting if you really need to do that.
You can invoke
Thread.sleep()
just before you exit your program: