The difference between sleep () and wait () in Java

Source: Internet
Author: User

1. These two methods come from different classes, and the sleep method belongs to the Thread,wait method that belongs to object.

2. The most important thing is that the sleep method does not release the lock, and the wait method frees the lock so that other threads can use the synchronization control block or method.

3. Wait, notify and notifyall can only be used in the synchronous control method (synchronized) or the sync control block, while sleep can be used anywhere.

4. Sleep must catch exceptions, while wait,notify and notifyall do not need to catch exceptions.

Detailed

The Sleep method is a method in the thread class, which means that a thread goes to sleep, waits for a certain amount of time, and then automatically wakes into a running state, but it does not go into a running state immediately, because other threads may be running and not dispatched to abort execution unless (a) "Wake up" Thread has a higher priority, and (b) The running thread is blocked for other reasons. After a thread object calls the Sleep method, it does not release all of the object locks he holds, so it does not affect the operation of other process objects. However, during the process of sleep, it is possible for other objects to call its interrupt (), to produce a interruptedexception exception, if your program does not catch this exception, the thread will terminate abnormally, into the terminated state, If your program catches this exception, the program will continue to execute the CATCH statement block (and possibly the finally statement block) and later code. Note that the sleep () method is a static method, which means that he is only valid for the current object and cannot get the T object into sleep by T.sleep ().

Wait belongs to the member method of object, and once an object calls the Wait method, the process must be woken up with the Notify () and Notifyall () methods. If a thread has a synchronization lock on one or some of the objects, after the call to wait (), the thread releases all of the synchronization resources it holds, not the object that is called the Wait () method. This allows other synchronized data in the object where the thread resides to be used by other threads. The wait () method can also produce interruptedexception, effects, and processing with the sleep () method during the wait process, possibly by other objects calling the interrupt () method.

The difference between sleep () and wait () in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.