1try
2{
3 Thread.sleep(1000);
4}
5catch(InterruptedException ex)
6{
7 Thread.currentThread().interrupt();
8}
9
1// Java program to demonstrate
2// sleep() method of TimeUnit Class
3
4import java.util.concurrent.*;
5
6class WaitASecond {
7 public static void main(String args[])
8 {
9 // Get time to sleep
10 long timeToSleep = 0L;
11
12 // Create a TimeUnit object
13 TimeUnit time = TimeUnit.SECONDS;
14
15 try {
16
17 System.out.println("Going to sleep for "
18 + timeToSleep
19 + " seconds");
20
21 // using sleep() method
22 time.sleep(timeToSleep);
23
24 System.out.println("Slept for "
25 + timeToSleep
26 + " seconds");
27 }
28
29 catch (InterruptedException e) {
30 System.out.println("Interrupted "
31 + "while Sleeping");
32 }
33 }
34}