In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the principle of Synchronized". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the principle of Synchronized"?
I. object lock and class lock
When synchronized modifies a normal method, an object, or a this, it is an object lock, and only the same object can be accessed.
When synchronized modifies static methods and .class, it is a class lock that can be accessed by different instances of the same class.
Public class Test {Object o1=new Object (); Object o2=new Object (); public synchronized void test1 () {int I = 5; while (iMel-> 0) {System.out.println (Thread.currentThread (). GetName () + ":" + I); try {Thread.sleep (500) } catch (InterruptedException ie) {}} public void test2 () {synchronized (this) {int I = 5; while (Imuri-> 0) {System.out.println (Thread.currentThread (). GetName () + ":" + I) Try {Thread.sleep;} catch (InterruptedException ie) {}} public static synchronized void test3 () {int I = 5; while (Imuri-> 0) {System.out.println (Thread.currentThread (). GetName () + ":" + I) Try {Thread.sleep (500);} catch (InterruptedException ie) {}} public void test4 () {synchronized (Test.class) {int I = 5; while (iMuk-> 0) {System.out.println (Thread.currentThread (). GetName () + ":" + I) Try {Thread.sleep;} catch (InterruptedException ie) {}} public void test5 () {synchronized (o1) {int I = 5 While (Thread.currentThread-> 0) {System.out.println (Thread.currentThread (). GetName () + ":" + I); try {Thread.sleep (500) } catch (InterruptedException ie) {}} public void test6 () {synchronized (O2) {int I = 5; while (iMury-> 0) {System.out.println (Thread.currentThread () .getName () + ":" + I) Try {Thread.sleep (500);} catch (InterruptedException ie) {}} public void test7 () {synchronized (O2) {int I = 5 While (Thread.currentThread-> 0) {System.out.println (Thread.currentThread (). GetName () + ":" + I); try {Thread.sleep (500) } catch (InterruptedException ie) {}} public static void main (String [] args) {final Test myt1 = new Test (); final Test myt2 = new Test () Thread test1 = new Thread (new Runnable () {@ Override public void run () {myt.test1 ();}}, "test1"); Thread test2 = new Thread (new Runnable () {@ Override public void run () {myt.test2 () }, "test2"); test1.start (); test2.start ();}}
When both threads use the object myt1 to access the method:
The first is the same lock (competition for locks); the third is the same lock; the second is the same lock; the third is the same lock; the other is the same lock.
When two threads use myt1 and myt2 to access the method, respectively:
1 myt1 2 is a different lock (because myt1 and myt2 are different objects), 3 myt1 4 is the same lock (because myt1 and myt2 are the same class), and 6 meme 7 is a different lock.
II. Sychronized principle
The implementation of the object lock uses monitorenter and monitorexit instructions, in which the monitorenter instruction points to the start position of the synchronous code block, and the monitorexit instruction indicates the end position of the synchronous code block. When executing the monitorenter instruction, the current thread will try to obtain the ownership of the monitor corresponding to the objectref (that is, the object lock). When the monitor entry counter of the objectref is 0, the thread can successfully obtain the monitor and set the counter value to 1. If the current thread already has ownership of objectref's monitor, it can reenter the monitor (reentrant will be analyzed later), and the value of the counter will be incremented by 1. If other threads already have ownership of objectref's monitor, the current thread will be blocked until the executing thread finishes execution, that is, the monitorexit instruction is executed, and the executing thread will release the monitor (lock) and set the counter value to 0, and the other threads will have a chance to hold the monitor.
The essence of monitor is the Mutex Lock of the operating system.
Every object in java can be a lock because of the design of the java object header:
The object header contains three parts: Mark Word (storing the object's own run-time data), Class Metadata Address (a pointer to store class metadata), and Array length (array length, which is available only to array types). Focusing on the Mark Word section, the Mark Word data structure is designed to be unfixed and varies according to the state of the object, as shown in the following table:
2. Copy the Mark Word in the object header to the lock record
3. After the copy is successful, the virtual machine will use the CAS operation to attempt to update the Mark Word of the object to the pointer to Lock Record, and point the owner pointer in Lock record to object mark word. If the update is successful, perform step 4, otherwise perform step 5.
4. If the update action is successful, then the thread owns the lock of the object, and the lock flag bit of the object Mark Word is set to "00", which means that the object is in a lightweight locked state. At this time, the state of the thread stack and object header is shown in the figure.
5. If the update operation fails, the virtual machine will first check whether the Mark Word of the object points to the stack frame of the current thread. If it means that the current thread already has the lock of the object, it can go directly to the synchronization block to continue execution. Otherwise, if multiple threads compete for locks, the lightweight lock will expand to the heavyweight lock, and the state value of the lock flag will become "10". What is stored in Mark Word is the pointer to the heavyweight lock (mutex), and the thread waiting for the lock will also enter the blocking state. The current thread tries to use spin to acquire the lock, which is the process of using a loop to acquire the lock in order to prevent the thread from blocking.
Lightweight lock release:
Previously, when it acquired the lock, it copied the markword of the lock object header, and when releasing the lock, if it found that another thread tried to acquire the lock while it was holding the lock, and the thread made changes to the markword, it would switch to the weight lock if the two comparisons were found to be inconsistent.
Summarize the locking process:
Check whether the ID of the current thread is in the Mark Word. If so, it indicates that the current thread is in a bias lock.
If not, use CAS to replace Mard Word with the ID of the current thread. If successful, it means that the current thread has acquired a bias lock and set it to the flag bit 1.
If it fails, the competition occurs, the bias lock is revoked, and the lock is upgraded to a lightweight lock.
The current thread uses CAS to replace the Mark Word of the object header with the lock record pointer, and if successful, the current thread acquires the lock
If it fails, it indicates that other threads are competing for the lock, and the current thread attempts to use spin to acquire the lock.
If the spin is successful, it is still lightweight.
If spin fails, upgrade to heavyweight lock
Https://blog.csdn.net/zqz_zqz/article/details/70233767
Https://blog.csdn.net/u012465296/article/details/53022317
Https://www.cnblogs.com/javaminer/p/3889023.html
At this point, I believe you have a deeper understanding of "what is the principle of Synchronized". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.