InterviewSolution
Saved Bookmarks
| 1. |
Compare sleep() and wait() methods in Java |
||||||||||||
|
Answer» The execution of a thread can be paused in a multithreading environment in Java using the sleep() or wait() methods. The thread is paused for the required time using sleep() while the thread goes into a wait state using wait() and can only be revived by calling NOTIFY() or NOTIFYALL(). Some of the differences between sleep() and wait() are given as follows:
An example of sleep() is given as follows: synchronized(LOCK) { Thread.sleep(1000); }An example of wait() is given as follows: synchronized(LOCK) { LOCK.wait(); } |
|||||||||||||