Forums Register Login
difference between wait() and sleep()

what is the difference between wait() and sleep() in threads.
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.
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.

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

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
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
Rocket Oven Kickstarter - from the trailboss

This thread has been viewed 798 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 18, 2018 06:57:56.