Difference between wait() and sleep()
A wait
can be "woken up" by another thread calling notify
on the monitor which is being waited on whereas a sleep
cannot. Also a wait
(and notify
) must happen in a block synchronized
on the monitor object whereas sleep
does not:
Object mon = ...;
synchronized (mon) {
mon.wait();
}
At this point the currently executing thread waits and releases the monitor. Another thread may do
synchronized (mon) { mon.notify(); }
(on the same mon
object) and the first thread (assuming it is the only thread waiting on the monitor) will wake up.
You can also call notifyAll
if more than one thread is waiting on the monitor – this will wake all of them up. However, only one of the threads will be able to grab the monitor (remember that the wait
is in a synchronized
block) and carry on – the others will then be blocked until they can acquire the monitor's lock.
Another point is that you call wait
on Object
itself (i.e. you wait on an object's monitor) whereas you call sleep
on Thread
.
Yet another point is that you can get spurious wakeups from wait
(i.e. the thread which is waiting resumes for no apparent reason). You should always wait
whilst spinning on some condition as follows:
synchronized {
while (!condition) { mon.wait(); }
}
Difference between wait and sleep
wait
waits for a process to finish; sleep
sleeps for a certain amount of seconds.
Thread.sleep and object.wait
One is used to synchronize Threads together while the other one is used to sleep for a given amount of time.
If you want to synchronize Threads together, user wait/notify. If you want to sleep for a known amount of time, use Thread.sleep.
wait()/wait(timeout)/sleep(timeout)?
wait(timeout) will return if the thread is still waiting after the timeout has elapsed. This is for hang notification, for low-power polling, etc etc. Sleep(timeout) won't wake up until the timeout has elapsed; wait(timeout) is either the notify() call or the timeout, whichever comes first.
Quoting from the JavaDoc:
This method causes the current thread (call it T) to place itself in
the wait set for this object and then to relinquish any and all
synchronization claims on this object. Thread T becomes disabled for
thread scheduling purposes and lies dormant until one of four things
happens:
- Some other thread invokes the notify method for this object and thread T happens to be arbitrarily chosen as the thread to be
awakened.- Some other thread invokes the notifyAll method for this object.
- Some other thread interrupts thread T.
- The specified amount of real time has elapsed, more or less. If timeout is zero, however, then real time is not taken into
consideration and the thread simply waits until notified.
Related Topics
How to Lock a File Using Java (If Possible)
Why Is the Java Main Method Static
Overriding Member Variables in Java ( Variable Hiding)
Sort on a String That May Contain a Number
How Does Java Garbage Collection Work With Circular References
Why Do We Need Break After Case Statements
Including Jars in Classpath on Commandline (Javac or Apt)
How to Solve Jdk Issue Unexpected At This Time
What Are the Rules For Evaluation Order in Java
"Error: Main Method Not Found in Class Myclass, Please Define the Main Method As..."
Why Does Java'S Hashcode() in String Use 31 as a Multiplier
How to Format a Duration in Java (E.G Format H:Mm:Ss)
How to Secure an API Rest For Mobile App (If Sniffing Requests Gives You the "Key")
How Does a Preparedstatement Avoid or Prevent SQL Injection
Get a List of Resources from Classpath Directory
How to Handle Events from Keyboard and Mouse in Full Screen Exclusive Mode in Java