- The sleep () method belongs to the thread class, and the Wait () method belongs to the object class.
- The sleep () method causes the program to pause execution for a specified time, giving out the CPU to other threads, but his monitoring state remains, and automatically resumes when the specified time is up, and the thread does not release the object lock during the call to the sleep () method. When the Wait () method is called, the thread discards the object lock, enters the waiting lock pool waiting for the object, and allows other threads to use the synchronization control block or method.
- The wait () method can only be used in a synchronous control method (synchronized) or in a synchronization control block, and sleep () is available anywhere.
- Sleep () must catch the exception, and wait () does not need to catch the exception.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
What is the difference between sleep () and wait () in interview Q&a-java