Difference between sleep () and wait () Methods

Source: Internet
Author: User

1. Different Sources:

The sleep () method is a static method of the thread class. Only a subclass of the thread class can call this method, even if the sleep method of thread B is called in thread A, it is still a to sleep. To sleep thread B, it is necessary to call sleep in the code of thread B.

1 static void sleep (long millis); // perform a sleep operation on the currently executed thread within the specified millisecond; 2 static void sleep (long millis, int Nanos ); // perform the sleep operation on the current thread within the specified milliseconds.

The wait () method comes from the ancestor of all Java classes of the object class. Therefore, any class can call the wait () method.

1 void wait (); // before other threads call the notify () or notifyall () method of this object, the current thread waits for 2 void wait (long timeout ); // wait until other threads call the notify () method or notifyall () method of this object, or before the specified time is exceeded.
3 void wait (long timeout, int Nanos); // in other threads, use the notify () method or notifyall () method of this object, or the current thread of another thread terminal, or the current thread is waiting before a certain amount of time has exceeded.

 

2,Most importantIs the sleep () method not availableRelease lockThe wait () method releases the lock so that other threads can use synchronous control blocks or methods. Just like squatting, even if you fall asleep on the toilet, you will not let it go. You can continue when you wake up. This is sleep (); half of it will be exchanged by the boss, giving up resources, this is wait (). It is the wake-up of the boss that decides you to kneel down again.

Therefore, it can usually be described as "Sleep" without transferring system resources; "wait" means entering the thread wait pool to wait, and transferring system resources. Other threads can occupy the CPU. Generally, wait does not apply a time limit because it is useless if the Running Resources of the wait thread are insufficient. Wait for other threads to call y/yyall to wake up all threads in the waiting pool, wait until the OS allocates system resources. Sleep (milliseconds) can automatically wake up with a specified time. If the time is not enough, interrupt () can only be called to force the interruption.

Thread. Sleep (0) is used to "trigger the operating system to immediately re-compete with the CPU ".


3,Scope of use:Wait, policy, and policyall can only be used in synchronous control methods or synchronization control blocks, while sleep can be used anywhere.

Synchronized (x) {x. y (); // or wait ()}

 


4. Sleep is required.Capture exceptionsWhile wait, policy, and policyall do not need to capture exceptions.

 

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.