You have a problem that you want to add a delay for a couple of seconds in your program. Using Thread.sleep()
method we can add delay in our application in a millisecond time. The Thread.sleep()
need to be executed inside a try-catch
block, and we need to catch the InterruptedException
. Let’s see the code snippet below.
package org.kodejava.lang;
public class DelayExample {
public static void main(String[] args) {
// This program demonstrate how to add a delay in our
// application.
for (int i = 0; i < 10; i++) {
// Print the value of i
System.out.println("i = " + i);
try {
// Using Thread.sleep() we can add delay in our
// application in a millisecond time. For the example
// below the program will take a deep breath for one
// second before continue to print the next value of
// the loop.
Thread.sleep(1000);
// The Thread.sleep() need to be executed inside a
// try-catch block and we need to catch the
// InterruptedException.
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024