当前位置:网站首页>Locking mechanism

Locking mechanism

2022-06-10 22:52:00 InfoQ



Today is June 8 Number , It is a big festival for e-commerce promotion , The origin of this festival is Jingdong 618 We are familiar with many scenes during the festival , Because we can encounter a lot of push messages from festivals ,

Today, let's talk about some things about the classic interview , The topic of this conversation is lock

Thread start -- The thread that currently obtains the lock resource

At first, it may be unlocked , Let's judge first

>  Priority is given to threads that have previously acquired lock resources ----- Of
Biased locking

If you don't succeed -- Upgrade to a   Lightweight CAS Optimistic lock of

A process of comparison and exchange , If it is not set successfully , Will spin ,

After many spins , Will be upgraded to a sychronized The heavyweight lock of

In this way, the quantization of lock performance can be guaranteed , Ensure the most appropriate concurrency of system data

First, let's talk about why there is a lock ?

Memory stores the local or static variables we need , When multiple threads enter, data exchange processing is performed , Or after data operation , We don't know about the data ,  There is no guarantee that the current thread will execute , Will it be robbed by other threads , And that leads to this , We need to add a locking mechanism , The fixed area can only allow one thread to enter ,

Here we can work with multiple threads or with variables , Use volatile Key words of , Prohibit command rearrangement , Data visibility but not atomicity ,unsafe Class

The data operation must be ,  For other threads

  • Secure and data visible ( If it changes , You need to notify other threads )
  • Atomic manipulation , Or success , Or failure
  • No reordering

Instruction rearrangement is the underlying assembly , And the underlying things of the operating system are based on the sorting of various languages , You can organize the data into idioms , Thus, the sentences are translated into program execution that can be recognized by the computer , This operation is called instruction rearrangement
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102135163146.html