File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between wait() and sleep()

 
kittu karan
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

what is the difference between wait() and sleep() in threads.
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gilles Marceau
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect..
 
manisha makwana
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic