1//deprecated in main thread:
2try {
3 Thread.sleep(1000);//time is in ms (1000 ms = 1 second)
4} catch (InterruptedException e) {e.printStackTrace();}
5
6//please use bellow instead:
7Timer timer = new Timer();
8timer.schedule(new TimerTask() {
9 @Override
10 public void run() {
11 //what you want to do
12 }
13}, 1000);//wait 1000ms before doing the action
1// Sleep 1 second
2TimeUnit.SECONDS.sleep(1);
3// Sleep 1 minute
4TimeUnit.MINUTES.sleep(1);
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}
1java.lang.Object.wait() causes current thread to wait until another thread
2invokes the notify() method or the notifyAll() method for this object.