The difference between Java multithreaded sleep (), yield (), wait (), join ()

Source: Internet
Author: User

Tag: Wait timed must not block Java end requires object

The difference between sleep (), yield (), wait (), join ()

1.sleep () Frees the CPU, does not release the lock

Causes the current thread to enter a blocking state that does not execute within the specified time.

2.yield () Frees the CPU, does not release the lock

Returns the current thread to its executable state, with the possibility of returning to the executable state and executing it immediately. Yield can only enable threads with the same sibling priority or higher priority to have an opportunity to execute.

3.wait () Release the CPU, release the lock

After calling the object's Wait method, the Rongguo notify () or Notifyall () method is required to wake up.

The invocation of the object's Wait (), notify (), Notifyall () is based on the assumption that the current thread must hold a lock on the object, so these three methods must be called in the synchronized function or synchronized block.

4.join () Release the CPU, release the lock

Wait for the thread that calls the join () method to execute the end, and then proceed down.

  

  

  

The difference between Java multithreaded sleep (), yield (), wait (), join ()

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.