The difference is as follows:
sleep() is a method of the thread class (Thread), which is used to suspend the execution of the thread for a specified time and give the execution opportunity to other threads. However, the monitoring status is still maintained and will be automatically restored after the time is up. Calling the sleep() method does not release the object lock.
(Recommended related tutorials: java introductory tutorial)
wait() is a method of the Object class. Calling the wait() method on the object causes the thread to give up the object lock and enter Waiting for the waiting lock pool of this object. Only after the notify method (or notifyAll) is issued for this object, this thread will enter the object lock pool and prepare to obtain the object lock and enter the running state.
(Video tutorial recommendation: java video tutorial)
The above is the detailed content of What is the difference between sleep() and wait() in Java?. For more information, please follow other related articles on the PHP Chinese website!