Java sleep, wait, yield, join, notify Chaos

Source: Internet
Author: User

① These two methods come from different classes, namely, sleep comes from the thread class, and wait comes from the object class.

Sleep is the static class method of thread, who calls who goes to sleep, even if you call B's Sleep method in a thread, actually a goes to sleep, to let the B thread sleep to call sleep in B's code.

② Lock: The most important thing is that the sleep method does not release the lock, and the wait method frees the lock so that other threads can use the synchronization control block or method.

Sleep does not sell system resources; Wait is the thread waiting for the pool to wait, to assign system resources, and other threads to consume the CPU. The general wait does not add a time limit, because if the wait thread runs out of resources, it is useless to wait for all threads in the Notify/notifyall wake-up waiting pool to be called by other threads before it enters the ready queue to wait for the OS to allocate system resources. Sleep (milliseconds) can be specified by time to wake it up automatically, if the time is less than the interrupt () force interrupt.

The role of Thread.Sleep (0) is "triggering the operating system to immediately re-compete with the CPU".

③ Use range: Wait,notify and notifyall can only be used in synchronous control methods or synchronization control blocks, and sleep can be used anywhere.

Synchronized (x) {
X.notify ()
or wait ()
}

Yield () temporarily hand over the CPU control, from the running state to the Runnalbe state, but it is still possible to be dispatched, the sleep () thread specifies sleep for a period of time, wait () in other threads call this object's notify () or The Notifyall () method can continue the main difference between the sleep () method and the Yeild () method in the execution thread: the 1.sleep () method will give the opportunity for other threads to run, regardless of the other thread's priority, and therefore will give the lower priority thread the chance to run; Yeild () Method only gives the opportunity to run with the same priority or higher than the thread. The 2.sleep () method declares the interruptionexception exception thrown, and the Yeild () method does not declare any exceptions.

The 3.sleep () method has higher portability than the Yeild () method.
The 4.sleep () method causes the thread to enter a blocking state, and the Yeild () method puts the thread into a ready state. The currently running thread can invoke the join () method of another thread, and the currently running thread will go to the blocking state until the other thread finishes running before it resumes running. Join () has two forms: public void Join () and public void join (long timeout) to set the time to block

1.sleep () causes a thread to hibernate for a period of time, after which the thread enters an executable state, but does not execute immediately, but only when it is called by the scheduler. During hibernation, the held "lock" is not released;

2.wait () causes the thread to hibernate for a period of time, if the parameter is set, the thread automatically enters the executable state when the time is up. If not, the Notify () method is required to invoke. Note: When the Wait () method and the Notify () method are used against the This object, the "lock" on the object is freedafter the Wait () method is called.

3.yield () gives the thread the right to abandon execution and enter an executable state, which means that the thread is likely to execute after the yield () method. Using the yield () method, the thread does not release its lock-held "lock".

The currently running thread can invoke the join () party method of another thread, and the currently running thread will go to the blocking state until the other thread finishes running and it resumes running.



Java sleep, wait, yield, join, notify Chaos

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.