Analysis of Sleep (), wait (), yield (), join () methods in Java

Source: Internet
Author: User

A thread exit is best implemented by itself, always checking a state in the running state, if the state is true, running all the time, and if the state variable is changed by the outside world, then the thread stops running.

1.sleep () method

Suspends execution of the currently executing thread for a specified period of time, but does not release the lock flag. It is not recommended.

Sleep () causes the current thread to enter a blocking state that will not be executed for a specified time period.

2.wait () method

Causes the current thread to wait before another thread calls the object's notify or Notifyall method. The thread releases the "lock flag" that it occupies, allowing other threads to seize the lock.

The current thread must have the current object lock. If the current thread is not the owner of this lock, a Illegalmonitorstateexception exception is thrown.

The waiting thread that wakes the current object lock uses the Notify or Notifyall method, and must also have the same object lock, or the illegalmonitorstateexception exception is thrown.

Waite () and notify () must be called in the synchronized function or synchronized block. If the call is made in the non-synchronized function or non-synchronized block, the illegalmonitorstateexception exception will occur at run time, although it can be compiled.

3.yield method

Pauses the currently executing thread object.

Yield () simply returns the current thread back to the executable state, so the thread executing yield () is likely to be executed immediately after entering the executable state.

Yield () only causes threads with the same priority or higher priority to have an opportunity to execute.

4.join method

Waits for the thread to terminate.

Wait for the thread that called the Join method to end, and then continue execution. such as: T.join ();//mainly used to wait for the T thread to run the end, without this sentence, main will be executed, resulting in unpredictable results.

Java enterprise-Class generic rights security framework source SPRINGMVC MyBatis or Hibernate+ehcache Shiro Druid Bootstrap HTML5

"Java Framework source code download"

Analysis of Sleep (), wait (), yield (), join () methods in Java

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.