Java Thread Synchronization principle

Source: Internet
Author: User
Tags object object sleep thread class
One. Java Thread Synchronization principle
Java assigns a monitor to each object object, and when a synchronization method (synchronized methods) is invoked by multiple threads, the object's monitor will handle the concurrent exclusive requirements for those accesses.
When a thread calls a synchronization method for an object, the JVM examines the object's monitor. If the monitor is not occupied, then the thread gets possession of the monitor, can continue to synchronize the object, and if the monitor is occupied by another thread, the thread is suspended until the monitor is released.
When a thread exits a synchronous method call, the thread releases the monitor, which allows other waiting threads to obtain the monitor so that calls to the synchronization method are executed.
Note: The Java object's monitor mechanism is not the same as the traditional critical-check code area technology. A Java synchronization method does not mean that only one thread is executing exclusively at the same time, but the critical check code area technology does ensure that the synchronization method is only executed by one thread at a time. The exact meaning of the Java Monitor mechanism is that at any one time, a synchronization method for a specified object object can only be invoked by one thread.
The Java object's monitor is followed by the object instance rather than following the program code. Two threads can simultaneously perform the same synchronization method, for example: A class is synchronized by Xmethod (), has a,b two object instances, one thread executes A.xmethod (), and another thread executes B.xmethod (). No conflict.

Two. The use of Wait (), notify (), Notifyall ()
The Obj.wait () method suspends this thread and releases the monitor for the Obj object. Can be awakened only if other threads call the Notify () or Notifyall () of the Obj object.
The Obj.notifyall () method wakes up all the sleeping threads associated with the Obj object, and then many of the awakened threads start competing for the Obj object's monitor possession, and the resulting thread continues to execute, but the other threads will continue to wait.
The Obj.notify () method is to randomly awaken a sleeping thread.
Wait,notify and Notifyall can only be used in synchronous control methods or in synchronized control blocks.
Such as:
Synchronized (x) {
X.notify ()
or wait ()
}
This explains why the call to wait (), notify (), Notifyall () thread must have the monitor possession of the obj instance object.
Each object instance has a waiting thread queue. These threads are the calling permission to wait on the synchronization method for the object. For a thread, there are two ways to get into this waiting thread queue. One is when the other thread executes the synchronization method, it also executes the synchronization method, and the other is calling the Obj.wait () method.
When a synchronization method completes or executes a wait (), another thread obtains access to the object. When a thread is placed in a wait queue, it must be ensured that the thread can be unfrozen through a call to notify () so that it can continue to execute.

Three. The difference between wait () and sleep ():
The sleep () function is a static function of the thread class, and does not involve the concept of synchronization between threads, just to allow a thread to get a sleeping time itself. Sleep can be used everywhere.
The wait function is a function of the object class, the problem to be solved is the synchronization between threads, which contains the acquisition and release of the synchronization lock, and the call to the wait method suspends the caller's thread until the other thread calls the Notify method of the same object to reactivate the caller.
Note that after the thread calls notify (), the Monitor is freed only after the thread has completely finished executing from the synchronized code, and the awakened thread can actually be executed.

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.