Comparison of wait() method and sleep() method

Comparison of wait() method and sleep() method

In fact, in theory, wait and sleep are completely incomparable, because one is used for communication between threads, and the other is to block threads for a period of time. The only similarity is that both threads can give up execution for a period of time. Using an example in life, there is a difference between eating sweets at weddings and eating sweets at home. To put it bluntly, giving up thread execution is just a small phenomenon of wait.

1. Summary

1. The lock needs to be requested before wait , and the wait execution will release the lock first , and then re-request the lock when it wakes up. This lock is the monitor lock on the wait object; 2. sleep ignores the existence of the lock , that is, the previously requested lock will not be released , and no lock will not be requested; 3. wait is a method of Object ; 4. sleep is a static state of Thread methods ;


Guess you like

Origin blog.csdn.net/qq_45665172/article/details/113818400