The Sleep method difference between wait and thread for Java object

Source: Internet
Author: User

First, here to introduce the wait, notify and notify all methods of the object

The wait, notify, and Notifyall methods are the final native methods of the object class. So these methods cannot be overridden by subclasses, the object class is a superclass of all classes, so there are three ways to call wait in the program.

Wait (); // Mode 1: this. Wait (); // Mode 2:super.wait (); // Mode 3
void Notifyall ()

Unbind all those threads that call the wait method on the object. This method can only be called inside a synchronous method or within a synchronization block . If the current thread is not the holder of a lock, the method throws a illegalmonitorstateexception exception.

void Notify ()

randomly selects a thread that calls the wait method on the object to unblock its state. This method can only be called inside a synchronous method or within a synchronization block . If the current thread is not the holder of a lock, the method throws a illegalmonitorstateexception exception.

void Wait ()

Causes the thread to enter a wait state until it is awakened by another thread through notify () or Notifyall. This method can only be called in a synchronous method . If the current thread is not the holder of a lock, the method throws a illegalmonitorstateexception exception.

void Wait (long Millis) and void wait (Long millis,int Nanos)

Causes the thread to enter a wait state until it is notified or passes the specified time. These methods can only be called in a synchronous method . If the current thread is not the holder of a lock, the method throws a illegalmonitorstateexception exception.

Object.wait () and Object.notify () and Object.notifyall () must be written inside the synchronized method or inside the synchronized block because: These methods require that the thread that is currently running the Object.wait () method has an object lock on the objects. even if you do know that the current context thread does have an object lock, you cannot write statements such as object.wait () in the current context.

Second, tell the difference between the two

1. The two methods come from different classes , namely, sleep comes from the thread class, and wait comes from the object class.

Sleep is the static class method of thread, who calls to sleep, even if the a line thread calls the sleep method of B, actually still a goes to sleep, to let the B thread sleep to call sleep in B's code.

2, The most important 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.

Sleep does not sell system resources; Wait is the thread waiting for the pool to wait, to assign system resources, and other threads to consume the CPU. The general wait does not add a time limit, because if the wait thread runs out of resources, it is useless to wait for all threads in the Notify/notifyall wake-up waiting pool to be called by other threads before it enters the ready queue to wait for the OS to allocate system resources. Sleep (milliseconds) can be specified by time to wake it up automatically, if the time is less than the interrupt () force interrupt.

The role of Thread.Sleep (0) is "triggering the operating system to immediately re-compete with the CPU".

3, use range:wait,notify and Notifyall can only be used in synchronous control method or synchronization control block, and sleep can be used anywhere
Synchronized (x) {
X.notify ()
or wait ()
}

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

The Sleep method difference between wait and thread for Java object

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.