Sleep and wait

Source: Internet
Author: User

I 've been in the exam twice, and I 've got a third wrong time. I changed my surname! Every day, the test interview turns around, and the mood is not found wrong! You can't be stupid anymore!

From: http://nneverwei.javaeye.com/blog/494280

Http://zhidao.baidu.com/question/75503928.html? Fr = ala0

 

1. The two methods are fromDifferent classesRespectively, Sleep comes from the Thread class, and wait comes from the object class.

Sleep is the static class method of thread. Whoever calls the sleep, even if the sleep method of B is called in thread a, actually a goes to bed, sleep should be called in B's code to make the B thread sleep.

2,Most importantYesNo sleep methodRelease lockThe wait method releases the lock so that other threads can use synchronous control blocks or methods.

Sleep does not assign system resources; wait enters the thread wait pool to wait, and transfers 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. Sort 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.