The difference between the wait() method and the sleep() method

The wait() method is a method in the Object class

The sleep() method is a method in the Thread class

Because the sleep() method is a method in the Thread class, it cannot change the machine lock of the object. Although the sleep() method is called to make the thread sleep, the machine lock of the object is not released, and other threads still cannot access the object.

The wait() method makes the thread sleep and also releases the machine lock. Other threads can access this object, and the waiting thread can be woken up by notify() and notifyAll() in the Object class.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325924208&siteId=291194637