wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes difference  between wait() and sleep() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "difference  between wait() and sleep()" Watch "difference  between wait() and sleep()" New topic
Author

difference between wait() and sleep()

kittu karan
Ranch Hand

Joined: Mar 09, 2008
Posts: 63
Hi,

what is the difference between wait() and sleep() in threads.
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
both methods make the thread into waiting state. but the difference is that on call of wait() the thread releases the lock on the object it has acquired whereas sleep() doesnt do.


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Gilles Marceau
Ranch Hand

Joined: Feb 17, 2007
Posts: 78
Another difference is that a thread executing the Thread.sleep method cannot
be awaken, whereas a thread executing the Object.wait() can be awaken by
another thread invoking the Object.notify() method.
On the other hand, both methods can throw an InterruptedException is the thread executing the method is interrupted.

Gilles


SCJP 1.5<br />SCJD 1.6<br />SCBCD in progress...
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Perfect..


Thanks<br />Dinesh
manisha makwana
Ranch Hand

Joined: Sep 14, 2007
Posts: 37
The main difference between two method is that sleep is a static method of class Thread and wait is method in Object class

Thread.sleep()
wait() non static method in Object class
Deepak Kadam
Greenhorn

Joined: Feb 25, 2008
Posts: 14
Originally posted by kiran peruka:
Hi,

what is the difference between wait() and sleep() in threads.


One more and main difference between wait() and sleep() is that wait() can be only used from synchronized block and main purpose of wait() is inter thread
communication.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between wait() and sleep()