• Post Reply Bookmark Topic Watch Topic
  • New Topic

difference between wait() and sleep()  RSS feed

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

what is the difference between wait() and sleep() in threads.
 
Ranch Hand
Posts: 131
  • 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.
 
Ranch Hand
Posts: 78
  • 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
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect..
 
Ranch Hand
Posts: 37
  • 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
 
Greenhorn
Posts: 14
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!