Java thread synchronization principle, the usage of wait,notify and the difference from sleep

Source: Internet
Author: User
Tags object object thread class

one. Java Thread Synchronization principle

Java assigns a monitor to each object object, and when a synchronization method (synchronized methods) or synchronization of one of the objects is called by more than one thread, the monitor of the object handles 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 class of Java a synchronization method does not mean that only one thread at a time is exclusively executed (synchronized methods of different objects can be executed concurrently), but the critical inspection Code area technique guarantees 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 (). Conflict.

two. Wait (), notify (), Notifyall () uses the
obj.wait () method to suspend this thread and release 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 sleepy threads that are blocked on obj objects, and then wakes up many threads competing for the monitor possession of the Obj object, and the resulting thread continues to execute, But other threads continue to wait. The
obj.notify () method is a random wake-up of a sleeping thread, and the procedure is similar to a Obj.notifyall () method.
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 ()
Sleeping () method is a static method of the thread class, and does not involve the concept of synchronization between threads, just to allow a thread to get a dormant time itself. Sleep can be used everywhere. The
Wait () method is a method of the object class, which solves the problem of synchronization between threads that contains the acquisition and release of a synchronization lock, and the call to the wait method suspends the caller's thread until another thread calls the notify of the same object The caller is reactivated by the () method.
Note: 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.

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.