Vernacular talk synchronized, CAS underlying principle, Lock locks and lock promotion principles

               目录

               1、乐观锁和悲观锁的概念

               2、synchronized底层的原理

               3.  CAS的原理

               4.  并发包下Lock锁和synchronized对比

               5.  锁升级原理
复制代码

Hello everyone, I Ninetowns four most Haoheng little ears.

Today we come to talk with vernacular synchronized, CAS underlying principle, Lock locks and lock promotion principles.

1, optimistic and pessimistic locking concept

A thread such as a variable to be modified during this modification, it is pessimistic psychology that other threads in this period, it is also possible to modify this variable, so add it to a variable lock to ensure that during its modification, do not the thread can not go to visit this variable. This lock is pessimistic locking. Heavyweight pessimistic lock is locked, objects on behalf of the synchronized keyword.

A thread such as to modify a variable, during this modification, it is optimistic psychological, that other threads in this period, not to modify this variable, so it might be a modify operation, will add to the variable lock. This lock is optimistic locking. Optimistic locking is a lightweight lock, on behalf of the object CAS.

Principle 2, synchronized underlying

First of all we need to know a concept --monitor.

Each object has an internal monitor, monitor which has a counter, starting from zero.

If this thread want to get the lock monitor, first determine the monitor counter is not 0, if 0, indicating no one acquires the lock, the thread can acquire the lock, and then counter by one; if not 0, description has been there are other thread has acquired the lock, the thread must block waiting.

Synchronized with the underlying principle is jvm command and monitor between the two.

generally synchronized to the object lock, lock class is the class object lock. If you use the synchronized keyword in jvm instruction underlying compilation, there will be two monitorenter and monitorexit instructions. Thread into synchronized code segments, monitorenter instruction execution monitor counter is incremented by 1, so that other threads found monitor counter is not 0, it is blocked waiting;

Thread a synchronized code segments, monitorexit instruction execution is to monitor counter by 1, so that other threads found monitor counter is 0, the lock can get, to monitor the counter is incremented, and then the execution of business logic.

The above is the underlying principle for the synchronized object, class locked.

The method of locking by not monitor command, but by ACC_SYNCHORNIZED keyword, determining whether the synchronization method.

3. CAS principle

CAS, Conmpare And Swap, English translates to "compare and exchange." It is a three-step process, the first step is to read the value, the second step comparison value, look at their own values, and just read a different, third step is to modify, if the value of reading the same with their own, on the amendment.

CAS implementation class is the most classic AtomicInteger.

For example, two threads to read on AtomicInteger plus 1, A thread the old value is 0, B threads read the old value is 0, A then execute CAS operations, more value, and the value just found myself reading the same, all is 0, then it modifies the value of 1; B threads execute CAS operations, more value, and the value just found myself reading is not the same, becomes 1, and it is equivalent to re-read the old values, old own memory value to 1, and then continue CAS operation.

CAS at the hardware level to the bottom of the atoms you are guaranteed the same time only one thread can execute CAS, then compare the first set, CAS same time other threads to execute at this time will fail.

CAS is a bug problem might arise ABA air circulation. If you want to solve the ABA problem, you can use AtomicStampedReference class, it created an internal version number in a similar way to solve the ABA problem.

4. Lock the lock and contract and Comparative Synchronized

I think the main difference between the two is the following four points;

1. First is synchronized java built-in keywords, is jvm level, Lock is a java class, is jdk level;

2.synchronized can not determine whether or not to acquire the lock state, Lock can determine whether to acquire the lock;

3.synchronized will automatically release the lock, Lock need (to release the lock unlock () method) in finally manually release the lock, or likely to cause thread deadlock;

4.Lock lock suitable for mass synchronization synchronization code, synchronized lock for a small amount of code synchronization issues.

5. Lock escalation principle

Began as a lock-free state, come on judgment would go out if there is a lock, a lock, then the very beginning of the lock is biased locking support. Biased locking lock this thread to get the current resource, I will give priority to acquire the lock to let him go, if it does not get into the lock, then upgrade to a lightweight, a cas lock that optimistic locking, optimistic locking it is a time to compare and exchange process, if no successful, it will be a spin before it escalated into such a heavyweight lock after a synchronized spin to a certain number, in which case he would ensure that the performance issues . If you think that is synchronized start such a heavyweight lock, that performance is relatively poor.

                        End
复制代码

About the author: Haoheng little ears, a Haoheng programmer. I want everyone in the technology world Haoheng, with the vision to see the world of technology. Welcome to scan the next Fanger Wei code, sustained attention, a big wave of original series is on the way

Guess you like

Origin juejin.im/post/5e685ba7f265da57685dd3f7