Threads: Pausing Execution with Sleep

public class SleepMessages {
    public static void main(String args[])
        throws InterruptedException {
        String importantInfo[] = {
            "Mares eat oats",
            "Does eat oats",
            "Little lambs eat ivy",
            "A kid will eat ivy too"
        };

        for (int i = 0;
             i < importantInfo.length;
             i++) {
            //Pause for 4 seconds
            Thread.sleep(4000);
            //Print a message
            System.out.println(importantInfo[i]);
        }
    }
}

Thread.sleep causes the current thread to suspend execution for a specified period.

The sleep method can also be used for pacing and waiting for another thread with duties that are understood to have time requirements.

Related concepts

Pausing Execution with Sleep

Threads: Pausing Execution with Sleep — Structure map

Clickable & Draggable!

Threads: Pausing Execution with Sleep — Related pages: