Delay In Java Program Example
Chapter:
Miscellaneous
Last Updated:
25-05-2016 18:46:43 UTC
Program:
/* ............... START ............... */
public class JavaThreadDelayExample {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
// Print the value of i
System.out.println("i = " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}
/* ............... END ............... */
Output
i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
Notes:
-
The Thread.sleep() need to be executed inside a try-catch.
- We need to catch the InterruptedException for Thread.sleep.
Tags
Delay In Java Program Example, Java, Thread