Thread synchronization and yield () wait () and notify (), Notifyall ()

Source: Internet
Author: User

1. Yield ()

This method is similar to sleep (), except that it is not possible for the user to specify how long to pause, and the yield () method only allows the same priority thread to have an opportunity to execute.

2. Wait () and notify (), Notifyall ()

These three methods are used to coordinate the access of multiple threads to shared data, so they must be used within the synchronized statement block. The Synchronized keyword is used to protect shared data and prevent other threads from accessing the shared data, but the process of the program is inflexible and how can other threads have access to shared data when the current thread has not exited the synchronized data block? At this point, use these three methods to control flexibly.

The wait () method suspends the current thread and releases the object lock, allowing other threads to enter the synchronized data block, where the current thread is placed in the object waiting pool. When the Notify () method is called, an arbitrary thread is removed from the object's wait pool and placed in the lock flag waiting pool, and only the lock flag waits for the thread to acquire the lock flag, and notify () does not work if there are no threads in the lock flag waiting pool.

Notifyall () removes all the threads waiting for that object from the object waiting pool and puts it in the lock flag waiting pool.

Note that these three methods are java.lang.Object methods.

Wait () and notify (), Notifyall () are methods of the object class

(1), the usual wait method has wait () and wait (long timeout);

void Wait () causes the current thread to wait before other threads call the Notify () method of this object or the Notifyall () method.

void wait (long timeout) causes the current thread to wait before another thread calls this object's notify () method or the Notifyall () method, or exceeds the specified amount of time.

After wait (), the thread releases the "lock flag" that it occupies, so that other shnchronized data in the object where the thread resides can be used by other threads.

Wait () H and notify () because they operate on the object's "lock flags," they 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.

(2), yield () no parameters

The Sleep method causes the currently running thread to be asleep for a period of time, entering the non-operational state, the length of time is set by the program, and the yield method allows the current thread to give up the CPU possession, but the time to make up is not settable.

Yield () also does not release the lock flag.

In fact, the yield () method corresponds to the following operations, first detect whether there is currently the same priority of the thread is in the same operational state, if so, the CPU's possession to the secondary thread, or continue to run the original thread, so the yield () method is called "concession", It gives the running opportunity to other threads of the same level.

The Sleep method allows a lower-priority thread to get a run opportunity, but when the yield () method executes, the current thread is still in a running state, so it is not possible to let the lower-priority thread get CPU possession at this time. In a running system, if a higher-priority thread does not call the sleep method and is not blocked by I/O, the lower-priority thread can only wait for all higher-priority threads to run to the end before the opportunity runs.

Yield () simply returns the current thread back to the executable state, and all threads that execute yield () are likely to be executed immediately after entering the executable state, so the yield () method only allows the same priority thread to have an opportunity to execute.

Thread synchronization and yield () wait () and notify (), Notifyall ()

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: 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.