Search code examples
javatimesystemdelay

Set a delay on System.exit(0)


I have the code below, however I want it in a function to be run 5 seconds after a System.out.println statement for examples

System.out.println("Well done");
//***5 second delay***
System.exit(0);`

How do I go about creating that 5 second delay between the println and the exit?


Solution

  • System.out.println("Well done");
    Thread.sleep(5000);
    System.exit(0);`
    

    Or to be little more specific, you can use the TimeUnit#sleep method on TimeUnit.SECONDS:

    System.out.println("Well done");
    TimeUnit.SECONDS.sleep(5);
    System.exit(0);`
    

    On another note, this information is trivial to find through google. The search term "java pause" or "java sleep" throws up a number of links that are relevant to your question.