Java - Pause execution using sleep
by
Jeremy Canfield |
Updated: August 30 2022
| Java articles
Here is an example of how to use Thread.sleep can be used to pause the execution of tasks. The value is in milliseconds, so 1000 milliseconds would be a 1 second pause. Notice also that throws InterruptedException is required. In this example, there will be a 1 second pause between printing Hello and World.
public class Main {
public static void main(String[] args) throws InterruptedException {
System.out.println("Hello");
Thread.sleep(1000);
System.out.println("World");
}
}
Did you find this article helpful?
If so, consider buying me a coffee over at