Both wait() and sleep() methods play an essential role in Java programming. Both methods look the same, but there is some difference in their working and existence within a program.
Let's see how they differ from each other:
wait() | sleep() |
---|---|
wait method is one of the methods of java.lang.Object class. | sleep method is one of the methods of java.lang.Thread class. |
Only a synchronized block or method can call the wait() method. | From any point or context in a program, you can call the sleep() method. |
notify() and notifyAll() methods are used for waking the waiting thread. | Interrupts are used for waking the sleeping thread. |
This method is usually implemented in a Java program for performing multi-thread-synchronization. | This method is generally applied for controlling the time of execution for a single thread. |