| Author |
wait() and sleep() methods
|
Saral Saxena
Ranch Hand
Joined: Apr 22, 2011
Posts: 202
|
|
Hi Folks,
Please advise me on the technical differences between wait() and sleep() methods, the technical justification that I was looking for..!!
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Saral Saxena wrote:
Please advise me on the technical differences between wait() and sleep() methods, the technical justification that I was looking for..!!
Although these two methods look very similar, they do two different things. One simple and the other somewhat complex.
The sleep() method puts the current thread to sleep(). This is needed when you want a thread to stop doing stuff for a fixed amount of time. Simple.
The wait() method is one-half of the wait/notify mechanism. And if you used other threading systems before, this mechanism provides the functionality of a condition variable. A condition variables purpose is to send notifications/signals between threads. And due to some race condition, it is tied to a lock, which it releases during the operation. This is somewhat complex.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Saral Saxena
Ranch Hand
Joined: Apr 22, 2011
Posts: 202
|
|
Henry Wong wrote:
Saral Saxena wrote:
Please advise me on the technical differences between wait() and sleep() methods, the technical justification that I was looking for..!!
Although these two methods look very similar, they do two different things. One simple and the other somewhat complex.
The sleep() method puts the current thread to sleep(). This is needed when you want a thread to stop doing stuff for a fixed amount of time. Simple.
The wait() method is one-half of the wait/notify mechanism. And if you used other threading systems before, this mechanism provides the functionality of a condition variable. A condition variables purpose is to send notifications/signals between threads. And due to some race condition, it is tied to a lock, which it releases during the operation. This is somewhat complex.
Henry
Hi Henry,
so you mean to say in sleep() threads sleep for the time period which is mentioned in arguements and it also keeps the lock with itself but in case of wait() the thread can come out before the time period mentioned in arguments and it also releases the locks ..??
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Saral Saxena wrote:
so you mean to say in sleep() threads sleep for the time period which is mentioned in arguements and it also keeps the lock with itself but in case of wait() the thread can come out before the time period mentioned in arguments  and it also releases the locks ..??
No. Both sleep() and wait() are interruptable, so they are the same in this regard.
To answer your question directly, the main difference between sleep and a timed wait, is that one takes the lock with it, and the other releases it. However, if that is the answer you take from this, then you are likely missing the point -- the wait() method is part of a condition variable mechanism which is integrated into how to use threading. I really recommend that you start with a tutorial on threads regarding this... the Sun/Oracle one is free .... http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html
Henry
|
 |
Saral Saxena
Ranch Hand
Joined: Apr 22, 2011
Posts: 202
|
|
the wait() method is part of a condition variable mechanism which is integrated into how to use threading
Hi Henry Wong,
I have gone with threading tutorial ..Can you please explain in detail wait() method is part of a condition variable mechanism which is integrated into how to use threading..If possible with a small example would be a great help..!! thanks in advance..!!
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Saral Saxena wrote:
I have gone with threading tutorial ..Can you please explain in detail wait() method is part of a condition variable mechanism which is integrated into how to use threading..If possible with a small example would be a great help..!! thanks in advance..!!
Don't know what to tell you. If you went though a large tutorial, with lots of examples, and didn't understand how wait/notify works.... I can't imagine how I can explain it to you, in the space of one or two paragraphs, when I have no idea what you didn't understand from the tutorial.
Henry
|
 |
mozammil muzza
Greenhorn
Joined: Dec 22, 2011
Posts: 29
|
|
Hi Saral,
The method sleep(1000); puts thread aside for exactly one second.
The method wait(1000), causes a wait of up to one second.
A thread could stop waiting earlier if it receives the notify() or notifyAll() call. The method wait() is defined in the class Object and the method sleep() is defined in the class Thread.
hope it worked little on your query
|
Khuda Haafiz Muzza 4 Java
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5886
|
|
Saral Saxena wrote:Hi Folks,
Please advise me on the technical differences between wait() and sleep() methods, the technical justification that I was looking for..!!
Did you read the javadocs for those two methods? The differences seem pretty clear to me. wait() requires that the current thread holds the object's monitor, releases that monitor, and waits until notitfy()/notifyAll() is called or some timeout is reached. sleep(), on the other hand, merely pauses the thread for some amount of time, and does not release any monitors.
All of this is in the javadocs.
So if you want your thread to pause for a fixed amount of time, use sleep(). If you want to release a monitor and wait for some other thread to tell you to start up again, use wait().
|
 |
 |
|
|
subject: wait() and sleep() methods
|
|
|