• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

wait() and sleep() methods

 
Ranch Hand
Posts: 203
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..!!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 32
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic