How do I add a delay in my program?

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();
            }
        }
    }
}
Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.