Pause Execution with Sleep In Java Example
Chapter:
Thread
Last Updated:
21-07-2016 10:45:37 UTC
Program:
/* ............... START ............... */
import java.util.*;
import java.text.*;
public class JavaSleepPauseExecution {
public static void main(String[] args) throws InterruptedException {
Calendar cal;
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));
Thread.sleep(5000);
cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));
}
}
/* ............... END ............... */
Output
Notes:
-
Thread.sleep() causes the current thread to suspend execution for a specified period.
- Thread.sleep() sends the current thread into the “Not Runnable” state for some amount of time. The thread keeps the monitors it has acquired - i.e. if the thread is currently in a synchronized block or method no other thread can enter this block or method.
Tags
Pause Execution with Sleep, Java