top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is difference between calling wait() and sleep() method in Java multi-threading?

+2 votes
548 views
What is difference between calling wait() and sleep() method in Java multi-threading?
posted Apr 10, 2015 by Roshni

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

Though both wait and sleep introduce some form of pause in Java application, they are tool for different needs. Wait method is used for inter thread communication, it relinquish lock if waiting condition is true and wait for notification when due to action of another thread waiting condition becomes false. On the other hand sleep() method is just to relinquish CPU or stop execution of current thread for specified time duration. Calling sleep method doesn’t release the lock held by current thread.

answer Apr 13, 2015 by Karthick.c
0 votes

Both are used to pause currently running thread, sleep() is actually meant for short pause because it doesn't release lock, while wait() is meant for conditional wait and that's why it release lock which can then be acquired by another thread to change the condition on which it is waiting.

See these Answers

Differences between wait() and sleep() methods in Java?
Difference between Wait and Sleep, Yield in Java?

answer Oct 14, 2016 by Ahana
...