What are the differences between Java multithreaded sleep and wait
This article will explain in detail what the difference between Java multithreaded sleep and wait is, Xiaobian thinks it is quite practical, so share it with you as a reference, I hope you can gain something after reading this article.
We all know that sleep is to let the thread sleep, to continue execution after the time, wait is to wait, need to wake up and continue execution, then these two methods in multi-threaded performance, what is the difference between them?
It can be summarized as follows.
use
From a usage perspective, sleep is a method of the Thread class, while wait is a method of the Object top-level class.
Sleep can be used anywhere, while wait can only be used in synchronization methods or synchronization blocks.
CPU and resource lock release
sleep,wait will suspend the current thread and give up CPU execution time after calling, but the difference is that sleep will not release the lock resources currently held by the object, and will continue to execute after the time, while wait will abandon all locks and need to notify/notifyAll to regain the object lock resources before continuing to execute.
exception catching
Sleep needs to catch or throw exceptions, while wait/notify/notifyAll doesn't.
About "Java multithreaded sleep and wait what is the difference" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.