File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "difference  between wait() and sleep()" Watch "difference  between wait() and sleep()" New topic

difference between wait() and sleep()

kittu karan
Ranch Hand

Joined: Mar 09, 2008
Posts: 63

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.

My Techie Blog - Java Questions
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.


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

Joined: Aug 06, 2007
Posts: 486

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

wait() non static method in Object class
Deepak Kadam

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

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
I agree. Here's the link:
subject: difference between wait() and sleep()
It's not a secret anymore!