What are the differences between sleep () Wait () yeild () and sleep?

Source: Internet
Author: User

1. The sleep () method will give other threads a chance to run without considering the priority of other threads. Therefore, it will give lower threads a chance to run. Yield () the method only gives the thread with the same or higher priority a chance to run.

2. When the thread executes the sleep (long millis) method, it will go to the blocking state. The parameter millis specifies the sleep time. When the thread executes the yield () method, will go to the ready state.
3. The sleep () method declaration throws an interruptedexception exception, while the yield () method does not declare that it throws any exception.
4. The sleep () method has better portability than the yield () method.
5. When the thread calls its own sleep () method or the join () method of other threads, it will enter the blocking state (this state stops the current thread, but does not release the resources occupied ). When sleep () ends or join () ends, the thread enters the runable state and continues waiting for the OS to allocate time slices;
6. When the thread calls the yeild () method, it means to discard the CPU time slice that is currently obtained and return to the runable state, which is in the same competition as other processes, the OS may continue to run the process;
7. When the thread enters the ready status, it finds that the resource to be called is synchronized. If the lock mark cannot be obtained, the thread enters the lock pool status immediately, wait for the lock mark to be obtained (other threads may already be waiting for the lock mark to be obtained in the lock pool, and they are in the queue state, that is, first come, first served). Once the thread obtains the lock mark, it is transferred to the operational status, waiting for the OS to allocate CPU time slices;
8. When a thread executes the wait () method, it enters a waiting pool related to the object and loses the lock of the object. When it is awakened by a notify () method, wait for the threads in the pool to be put into the lock pool. This thread acquires the lock from the lock pool and then returns to the interrupt site before wait.
9 when the thread calls the wait () method, it enters the waiting queue (entering this status will release all the resources occupied, different from the blocking Status). After entering this status, automatic wake-up is not allowed. You must rely on other threads to call the notify () or notifyall () method to wake up automatically (wait (1000) (due to notify () only wake up a thread. However, we cannot determine which thread to wake up, and the thread we need to wake up may not be able to wake up.) in actual use, policyall () is generally used (), method To wake up all threads (). When the thread is awakened, it enters the lock pool and waits for the lock mark to be obtained.

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.