How to pause my Java program for 2 seconds

java pause program until key pressed
thread.sleep in java for 1 minute
thread.sleep java
java wait function
delay in java without using thread
pause for a second java
wait for 10 seconds in java
java pause 30 seconds

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?

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.

How to Delay Code Execution in Java, It is relatively common for Java programs to add a delay or pause in 2. A Thread-Based Approach. When a Java program runs, it spawns a XXX.sleep(y)​, where XXX is the time unit to sleep for (SECONDS, MINUTES, etc.)  You can use Thread.currentThread().sleep(2000) to pause the current thread for 2 seconds (2000 milleseconds). You should surround this with a try/catch in case of InterruptedException s. share | improve this answer answered Apr 19 '17 at 23:23

You can find a similar post here: How to delay in Java?

Basically what says in the old post. You could use java.util.concurrent.TimeUnit

    import java.util.concurrent.TimeUnit;

    if (doAllFaceUpCardsMatch == false) {
        TimeUnit.SECONDS.sleep(2);
        concentration.flipAllCardsFaceDown();
    } else {
        concentration.makeAllFaceUpCardsInvisible();
    }

Java Thread suspend() Method with Examples, Thread.sleep causes the current thread to suspend execution for a specified period. time available to the other threads of an application or other applications that The SleepMessages example uses sleep to print messages at four-second  Sometimes you want your function to pause execution for a fixed amount of seconds or milliseconds. In a programming language like C or PHP, you’d call sleep(2) to make the program halt for 2 seconds. Java has Thread.sleep(2000), Python has time.sleep(2), Go has time.Sleep(2 * time.Second).

You can use Thread.currentThread().sleep(2000) to pause the current thread for 2 seconds (2000 milleseconds). You should surround this with a try/catch in case of InterruptedExceptions.

Java.lang.Object.wait() Method, There are multiple ways to pause or stop the execution of currently running Here is our sample Java program to pause a running thread using Thread.sleep() and The second thread is "T1", which we have created and it is used to run our game 2) You can wake-up a sleeping thread by calling interrupt() method on the  I this video how to pause or sleep a Java program for a certain amount of time using the Thread.sleep() method. Thanks for watching!!!! Comment, like, and su

For those just wanting a quick hack without having to bring in a library...

public class Timing {
    public static void main(String[] args) {
            int delay = 1000; // number of milliseconds to sleep

            long start = System.currentTimeMillis();
            while(start >= System.currentTimeMillis() - delay); // do nothing

            System.out.println("Time Slept: " + Long.toString(System.currentTimeMillis() - start));
    }
}

For high precision 60fps gaming this probably isn't what you want, but perhaps some could find it useful.

Pausing Execution with Sleep (The Java™ Tutorials > Essential , Here is a simple program where Thread.sleep() is used to pause the main thread execution for 2 seconds. ThreadSleep.java. package com.journaldev.threads;  This code will log “Hello”, wait for two seconds, then log “World!” Under the hood we’re using the setTimeout method to resolve a Promise after a given number of milliseconds.

How to pause Thread in Java using Sleep() and TimeUnit Example , 2. Thread.sleep. JavaDelayExample2.java. package com.mkyong; import java.util​.Date; public class JavaDelayExample2 { public static void  Pausing Execution with Sleep. Thread.sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system. The sleep method can also be used for pacing,

Thread.sleep() in Java - Java Thread sleep, //Java code to pause the execution of a thread class ThreadPause { // method to pause the string // here we will pass the time in seconds public  This code pauses the current thread for about 2 seconds (or 2000 milliseconds). After that amount of time, the thread returns to continue running normally. InterruptedException is a checked exception so you must handle it. This exception is thrown when the thread is interrupted by another thread. Let’s see a full example.

Java – How to delay few seconds – Mkyong.com, In this post, we will see how to temporarily cease execution of a Java program for specified time. There are two in-built mechanism to sleep in Java. one can use Thread.sleep method which takes takes in the number of milliseconds to sleep. Consider below code, which calls Thread.sleep method to sleep for five seconds. HOW CAN I DELAY A JAVA PROGRAM FOR FEW SECONDS Skip navigation Sign in. Advanced Java: Multi-threading Part 8 - Wait and Notify - Duration: Java Programming Tutorial

Comments
  • Possible duplicate of How to delay in Java?
  • Thread.sleep would be the obvious choice, however, if you're using a GUI like Swing or JavaFX, you shouldn't do this from within the context of their dispatching threads, that would be really, really bad. Most UI frameworks will have their own defined mechanisms for doing this, but we don't have that information
  • When I do Thread.sleep() I get: "error: unreported exception InterruptedException; must be caught or declared to be thrown Thread.sleep(2000);" when compiling
  • @Ethanph89 well, the compiler is telling you exactly what you have to do: catch the InterruptedException, or declare it to be thrown.
  • Incidentally, don't explicitly compare to true and false.
  • Thanks for the help! Like I said, I'm new to Java (and most programming in general)- how would I catch the Exception?
  • This has a wasteful high CPU usage.