Thursday, 24 March 2016

Difference between Wait and Sleep, Yield in Java

The difference between wait and sleep or the difference between sleep and yield in Java are one of the popular core Java interview questions and asked on multi-threading interviews. Out of three methods which can be used to pause a thread in Java, sleep() and yield() methods are defined in thread class while wait() is defined in the Object class, which is another interview question. The key difference between wait() and sleep() is that former is used for inter-thread communication while later is used to introduced to pause the current thread for a short duration. This difference is more obvious from the fact that, when a thread calls the wait() method, it releases the monitor or lock it was holding on that object, but when a thread calls the sleep() method, it never releases the monitor even if it is holding. 
Read more »

No comments:

Post a Comment