Sleep Function Example Java Program


Definition

Sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.

Syntax

Thread.sleep(Sleep_time);

Sleep Function Example Program

public class SleepFunctionDemo {
    public static void main(String args[])
        throws InterruptedException {
        String[] str = {
            "This is statement 1",
            "This is statement 2",
            "This is statement 3",
            "This is statement 4"
        };
        for (int i = 0;i < str.length;i++) {
            Thread.sleep(4000);
            System.out.println(str[i]);
			System.out.println("After 4 seconds....");

        }
    }
}

Sample Output

Output is:
This is statement 1
After 4 seconds....
This is statement 2
After 4 seconds....
This is statement 3
After 4 seconds....
This is statement 4
After 4 seconds....