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 build simple search page using ZK and Spring Boot? - March 8, 2023
- How do I calculate days between two dates excluding weekends and holidays? - January 12, 2023
- How do I discover the quarter of a given date? - January 11, 2023