The difference between wait and sleep

wait

Acting object: The wait method is defined in the Object class and acts on the object itself.

Release lock resources: Yes

Wake-up method: Other threads call the notify() or notifyall() method of the object

Method attributes: instance methods

Synchronization: The wait method can only be called in a synchronization context, otherwise an lllegalMonitorStateException exception is thrown

sleep

Action object: The sleep method is defined in the Java.lang.Thread class and acts on the current thread.

Release lock resources: No

Wake-up method: timeout or call interrupt() method

Method attributes: static method

Synchronous: no need to call in synchronized method or synchronized code block

Guess you like

Origin blog.csdn.net/m0_58823014/article/details/126135401