This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wait() and sleep() methods

 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 32
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic