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 create a string of repeated characters? - September 1, 2023
- How do I convert datetime string with optional part to a date object? - August 28, 2023
- How do I split large excel file into multiple smaller files? - April 15, 2023