How to pause my Java program for 2 seconds

Ethanph89 picture Ethanph89 · Apr 20, 2017 · Viewed 55.7k times · Source

I'm new to Java and making a small game for practice.

if (doAllFaceUpCardsMatch == false) {
        //run pause here//
        concentration.flipAllCardsFaceDown();
} else {
        concentration.makeAllFaceUpCardsInvisible();
}

I want to pause the game for two seconds here before it does

concentration.flipAllCardsFaceDown();

How would I go about pausing it?

Answer

Ashish Gupta picture Ashish Gupta · Apr 20, 2017

You can use:

 Thread.sleep(2000);

or

java.util.concurrent.TimeUnit.SECONDS.sleep(2);

Please note that both of these methods throw InterruptedException, which is a checked Exception, So you will have to catch that or declare in the method.

Edit: After Catching the exception, your code will look like this:

if (doAllFaceUpCardsMatch == false) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        concentration.flipAllCardsFaceDown();
} else {
        concentration.makeAllFaceUpCardsInvisible();
}

Since you are new, I would recommend learning how to do exception handling once you are little bit comfortable with java.