Java Multithreaded Series Foundation article Wait/notify/sleep/yield/join

Source: Internet
Author: User
Tags thread class

Wait ()/notify ()/notifyall () in the 1.Object class

Wait (): leave the current thread in the waiting state and release the held object lock until another thread calls this object's thread notify ()/notifyall () method, and the current thread is awakened into the runnable state.
Wait (long timeout): leave the current thread in the timed waiting state and release the held object lock until another thread calls this object's thread notify ()/notifyall () method or time expires. The current thread is awakened into the runnable state.
Wait (long timeout, int nanos): leave the current thread in the Timed waiting state, until other threads call the Notify () method of this object or the Notifyall () method, or some other thread interrupts the current thread, or the time expires, the current thread is awakened into the runnable state.
notify (): randomly wakes a single thread waiting on this object monitor (does not immediately release the held object lock until the synchronized method or syntax block finishes executing to actually release the lock).
notifyall (): randomly wakes all the threads that are waiting on this object monitor (does not immediately release the held object lock until the synchronized method or syntax block finishes executing to actually release the lock).

Sleep ()/yield ()/join () in the 2.Thread class

sleep (): lets the current thread pause for a specified time (in milliseconds), the sleep method only temporarily yields the CPU's execution, and does not release the lock in the synchronized-modified synchronization method or code block.
yield (): suspends the current thread so that other threads have an opportunity to execute, but cannot specify a time to pause, and there is no guarantee that the current thread will stop immediately and give up the CPU's execution temporarily.
join (): let the current parent thread wait for the child thread to execute before executing, but one thing to note is that the join here only calls the Wait method, but there is no corresponding notify method, because the thread's Start method does the corresponding processing, So when the join thread finishes executing, the main thread is automatically awakened to continue execution.

Java Multithreaded Series Foundation article Wait/notify/sleep/yield/join

Related Article

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.