5

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

EmaadP
  • 160
  • 1
  • 1
  • 10
  • This is interesting, but why on earth would you exit from a running VM? – Eugene Apr 01 '13 at 16:31
  • 2
    0 `does not stand for seconds`. In System.exit(X), X is the exit code from your program (what it will report to the calling shell), it has nothing to do with time. – KevinDTimm Apr 01 '13 at 16:32
  • @Eugene I'm trying to create a game in which you have to answer math questions quickly. – EmaadP Apr 01 '13 at 21:01

5 Answers5

13

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));
    }

}

and then you can just called TimedExit()

nate_weldon
  • 2,289
  • 1
  • 26
  • 32
  • 3
    Thanks, I used this idea and updated it to the java 8 way. It is now a one-liner: Executors.newSingleThreadScheduledExecutor().schedule(() -> System.exit(0), 60, TimeUnit.SECONDS); – Paul S Nov 27 '18 at 14:45
8

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

// Your code goes here.

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

System.exit(0);
Juvanis
  • 25,802
  • 5
  • 69
  • 87
1

From System.exit(int) method documentation:

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

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)

Francisco Spaeth
  • 23,493
  • 7
  • 67
  • 106
0

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:

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.

Avi
  • 351
  • 1
  • 4
0
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

Community
  • 1
  • 1
tarunajain
  • 89
  • 1
  • 3