wait java code example
Example 1: java wait a second
import java.util.concurrent.*;
class WaitASecond {
public static void main(String args[])
{
long timeToSleep = 0L;
TimeUnit time = TimeUnit.SECONDS;
try {
System.out.println("Going to sleep for "
+ timeToSleep
+ " seconds");
time.sleep(timeToSleep);
System.out.println("Slept for "
+ timeToSleep
+ " seconds");
}
catch (InterruptedException e) {
System.out.println("Interrupted "
+ "while Sleeping");
}
}
}
Example 2: wait method in java
java.lang.Object.wait() causes current thread to wait until another thread
invokes the notify() method or the notifyAll() method for this object.