Hi, I'm Mic
A fan who has been working for 7 years came to me and said that he had been confused by various locks recently.
For example, shared locks, exclusive locks, biased locks, lightweight locks, spin locks, heavyweight locks,
Gap locks, near-key locks, intention locks, read-write locks, optimistic locks, pessimistic locks, table locks, and row locks.
Then I went to the interview two days ago and was asked about the preference for locks and lightweight locks, but I didn't answer.
ok, about the principle of Synchronized lock upgrade, see the answers of ordinary people and experts.
Ordinary people:
I think the purpose of introducing these locks should be to take into account the performance problem.
Because I remember it seems to say that if you add heavyweight locks in Synchronized, there will be this blocking in this thread, which will affect performance. Therefore, a mechanism for biased locks was introduced.
Then the upgrade means that when we acquire locks, we compete for locks in the way of biased locks, lightweight locks and heavyweight locks.
Expert:
OK, interviewer.
Before the jdk1.6 version, Synchronized implemented lock competition between threads by means of heavyweight locks.
The reason why it is called a heavyweight lock is that its bottom layer relies on the operating system's Mutex Lock to implement the mutual exclusion function.
Mutex is a system method. Due to permission isolation, an application needs to switch to the kernel mode for execution when calling a system method.
This involves the switch from user mode to kernel mode, which will bring performance loss.
In the jdk1.6 version, synchronized adds a lock escalation mechanism to balance data security and performance. Simply put, when a thread accesses a synchronized block of synchronized code, the synchronized
In the case of thread competition, it will first try to ensure thread safety without adding heavyweight locks. Therefore, the mechanism of biased lock and lightweight lock is introduced.
Biased lock is to directly bias the current lock to a certain thread. In short, it is to modify the biased lock flag through CAS. This kind of lock is suitable for the scenario where the same thread applies for the same lock resource multiple times and there is no competition from other threads.
Lightweight locks can also be called spin locks, based on an adaptive spin mechanism that competes for locks through multiple spin retries. The advantage of spinlock is that it avoids the performance overhead caused by switching from user mode to kernel mode.
- After Synchronized introduces the lock escalation mechanism, if there are threads competing for locks:
First, synchronized will try to use the biased lock to compete for lock resources. If the biased lock can be competed, it means that the lock is successfully returned and returned directly. If the competition lock fails, it means that the current lock has been biased towards other threads.
The lock needs to be upgraded to a lightweight lock. In the lightweight lock state, the threads competing for the lock try to preempt the lock resource according to the number of adaptive spins. If the lock is still not competed in the lightweight lock state,
It can only be upgraded to a heavyweight lock. In the heavyweight lock state, threads that do not compete for the lock will be blocked, and the thread state is Blocked.
A thread in the lock-waiting state needs to wait for the thread that acquires the lock to trigger a wake-up.
In general, the design idea of Synchronized's lock escalation, in my opinion, is essentially a balance between performance and security, that is, how to ensure thread safety without locking.
This kind of thinking is common in the programming field. For example, MVCC in Mysql uses version chains to solve the competition problem of multiple parallel transactions.
The above is my understanding of the problem.
Summarize
Locks are very common in programs. We deal with locks almost every day, such as row locks and table locks in Mysql.
So its importance is self-evident.
We can clearly see from the master's answer that the master's understanding of Synchronized is very high.
Friends who like my works remember to like, favorite and follow.
Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source forMic带你学架构
!
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。