1.1 What is a lock

In computer science , lock (lock) Or mutually exclusive (mutex) It's a synchronization mechanism , Used to enforce access restrictions on resources in environments with many execution threads . Locks are designed to enforce mutual exclusion 、 Concurrency control strategy .

Locks usually require hardware support to be implemented effectively . This support usually takes the form of one or more atomic instructions , Such as "test-and-set", "fetch-and-add" or "compare-and-swap"”. These instructions allow a single process to test whether a lock is idle , If idle , The lock is obtained through a single atomic operation .

1.2. An important property of locks Particle size

Before introducing lock granularity , You need to understand three concepts about locks :

1、 Lock overhead lock overhead Locks take up memory space 、 cpu Initialize and destroy locks 、 Time to acquire and release the lock . The more locks a program uses , The larger the lock overhead is

2、 Lock competition lock contention A process or thread attempts to acquire a lock held by another process or thread , There will be lock competition . The smaller the lock granularity , The less likely lock competition will occur

3、 Deadlock deadlock When each of at least two tasks is waiting for a lock held by another task, the lock granularity is a measure of the amount of data protected by the lock , Coarse grained locks are usually chosen ( The number of locks is small , Each lock protects a large amount of data ), When a single process accesses protected data, the lock overhead is small , But when multiple processes access at the same time, the performance is poor . Because it increases the lock competition . contrary , Use fine-grained locks ( Lots of locks , Each lock protects a small amount of data ) Increases lock overhead but reduces lock contention . For example, in the database , The granularity of the lock is table lock 、 Page locks 、 Row lock 、 Field lock 、 Part of the field is locked

Related terms  Critical Section( A critical region )、 Mutex/mutual exclusion( mutex )、 Semaphore/binary semaphore( Semaphore )

2. The type of lock

2.1. Exclusive lock / Shared lock

Exclusive lock means that the lock can only be held by one thread at a time . (ReentrantLock、 Synchronized)

Shared lock means that the lock can be held by multiple threads . (ReadWriteLock)

The mutex / Read-write lock

Exclusive lock / Shared lock, in a broad sense , The mutex / Read and write locks correspond to specific implementation respectively . stay Java In such as ReentrantLock It's mutex ( Exclusive lock ), ReadWriteLock Read and write lock (  Read lock is a shared lock , Write lock is exclusive lock ). Exclusive and shared locks are also available through AQS To achieve .

Lock escalation : Read lock to write lock ( I won't support it )

Lock down : Write lock to read lock ( Support )

2.2. Read-write lock ReentrantReadWriteLock

high 16 Bit for write lock , low 16 Bit stands for read lock

2.2. Fair lock / Not fair lock

Fair lock means that multiple threads acquire locks according to the order of applying for locks .

Unfair lock refers to the order in which multiple threads acquire locks, not the order in which they apply for locks , It is possible that the thread that applies later acquires the lock prior to the thread that applies first . There's a risk of starvation .

about Java ReentrantLock And in terms of , Specify whether the lock is a fair lock through the constructor , Default is unfair lock . The advantage of unfair lock is that throughput is larger than fair lock .

about Synchronized for , It's also a kind of unfair lock . Because it doesn't look like ReentrantLock It's through AQS To control the acquisition of lock by thread , So there's no way to make it a fair lock .

2.3. Reentrant lock

Reentrant locks are also known as recursive locks , When the same thread acquires a lock in an outer method , When entering the inner layer, the method will automatically acquire the lock .

ReentrantLock、ReadWriteLock and Synchronized Are reentrant locks . One of the advantages of reentrant lock is that it can avoid deadlock to some extent

Like the code above , If synchronized If it's not a reentrant lock ,testB Will not be executed by the current thread , So it's a deadlock .

It should be noted that , The number of times of locking and unlocking should be equal .

C==0 Indicates that the lock has not been obtained ,Else Indicates that the lock has been obtained , Right now state Add 1, Corresponding , Every time the lock is released, it's going to state reduce 1

2.4. Optimism lock / Pessimistic locking

Optimism lock / Pessimistic locks are not specific types of locks , It's about looking at concurrency .

Pessimistic lock thinks that there are many concurrent update operations , Take lock operation , If it's not locked, there's bound to be a problem

Optimistic lock thinks that there are not many concurrent update operations , No need to lock . Optimistic lock in database is usually implemented by version number ,Java Can be used in CAS Achieve optimistic lock .

cas Lock free mechanism : namely compare and swap perhaps compare and set, There are three operands involved , The memory value of the data , Expected value , The new value . When updates are needed , Determine whether the current memory value is equal to the previous value , If equal , Update... With new value , If it fails, try again , In general, it's a spin operation , That is, trying again and again .

About cas For details, please refer to What is? cas Mechanism ?

2.5. Section lock

Sectional lock is a kind of lock design , It's not a specific lock . about ConcuttentHashMap It is to realize efficient concurrent operation through segmented lock .

2.6. spinlocks

Spin lock means that the thread trying to acquire the lock will not block , Instead, try to acquire the lock in a circular way . The advantage is less context switching , The disadvantage is that it takes up all the time CPU resources .

2.7. Biased locking / Lightweight lock / Heavyweight lock

These three locks refer to the state of the lock , And it's for Synchronized. stay Java 5 To achieve high efficiency by introducing lock upgrade mechanism Synchronized. The three lock states are indicated by the fields in the object header of the object monitor .
Biased lock means that a piece of synchronous code is always accessed by a thread , Then the thread will automatically acquire the lock . Reduce the cost of lock acquisition .
Lightweight lock means when the lock is biased , Accessed by another thread , Biased locks will be upgraded to lightweight locks , Other threads try to acquire the lock by spinning , It won't block , Improve performance .
A heavyweight lock is when a lock is a lightweight lock , The other thread is spinning , But spin doesn't last , When you spin a certain number of times , We haven't got the lock yet , It's going to go into a block , The lock expands to a heavyweight lock . Heavyweight locks will block other threads applying for it , Performance degradation .

Here is a detailed explanation

jdk1.6 Chinese vs Synchronized Lock optimization , First, let's look at the object header (Mark Word):

Runtime JVM Memory layout

Mark Word Flag bit storage in different lock states

from jdk1.6 In order to reduce the performance cost of acquiring and releasing locks , Introduced “ Biased locking ” and “ Lightweight lock ”. There are four states of lock , From low to high, they are : No lock state 、 Biased lock state 、 Lightweight lock state and heavyweight lock state . With the competition, the lock status gradually escalates 、 Locks can be upgraded but not degraded .

Biased lock acquisition and revocation

HotSpot The author found that , Most of the time , Locks don't just have multithreaded competition , And it is always obtained by the same thread many times , In order to make the cost of thread getting lock lower, bias lock is introduced .

Threads 1 Check for Mark Word Whether threads are stored in 1, If not CAS Operation will Mark Word Thread in ID Replace with thread 1. here , The lock is biased toward the thread 1, Later, when the thread enters the synchronization block, it does not need to be synchronized CAS operation , Just a simple test Mark Word Whether to store a biased lock to the current thread in , If successful, the thread has acquired the lock . If you fail , You need to test it again Mark Word Whether the bias lock flag in is set to 1( Is it biased lock ), If not set , Then use CAS Competitive lock , If set , Then try to use CAS Point the biased lock to the current thread

The competition result of biased lock : 

Depending on whether the thread holding the biased lock survives

1. If not active , The bias lock is undone to the unlocked state , And then to other threads

2. If the thread is still alive , Upgrade to lightweight lock

Bias locked in Java6 and Java7 It is on by default , But it doesn't activate until a few seconds after the application starts , Turn off the delay if necessary :


If it is determined that all locks in the application are normally in contention , Can pass JVM Valuepoint close bias lock :

-XX:-UseBiasedLocking=false, Then the program will enter the lightweight lock by default .

-XX:BiasedLockingStartupDelay=0 -XX:+TraceBiasedLocking

Lightweight lock expansion :

1. Thread before executing synchronization block ,JVM A space for storing lock records is created in the current stack frame (Lock record), And Mark Word Copy to lock record (Displaced Mark Word).

2. Then the thread tries to use CAS Put the Mark Word Replace with a pointer to the lock record

3. If it works , Current thread gets lock , If you fail ( Spin over 10 Time ), Indicates that other threads are competing for locks , The current thread is trying to use spin to get the lock

Biased locking 、 Lightweight lock 、 The advantages and disadvantages of heavyweight locks

1. The purpose of biased locking is to prevent a thread from getting it over and over again / Performance cost when releasing the same lock , If it's still the same thread getting the lock , When you try to skew the lock, you go straight to the synchronization block , You don't need to get the lock again .

2. Both lightweight locks and spin locks are designed to avoid calling mutually exclusive operations at the operating system level directly , Because suspending a thread is a resource intensive operation .

To avoid heavy locks as much as possible ( Operating system level mutual exclusion ), First try lightweight locks , Lightweight locks will try to use CAS Operation to obtain lock , If lightweight lock acquisition fails , Indicates competition . But maybe you'll get the lock soon , They try spin lock , Do a few empty loops on the thread , Try to get the lock every time you loop . If the spin lock also fails , Then it can only be upgraded to heavyweight lock .

3. You can see the deflection lock , Lightweight lock , Spin locks are optimistic locks .

Escape analysis :

Escape analysis : Let's talk about it more generally , When an object's pointer is referenced by more than one method or thread , We say that this pointer has escaped , Must be in JIT Complete in

If you can prove that an object does not escape from a method or thread , That is, other methods or threads cannot access this object in any way , You can do some efficient optimization for this variable :

  • On the stack : Assign local objects that will not escape to the stack , The object will be destroyed automatically with the end of the method , Reduce the pressure on the garbage collection system .
  • Synchronous elimination : If there is no thread escape for this variable , That is, it cannot be accessed by other threads , Then there is no competition for the reading and writing of this variable , You can eliminate synchronization ( Synchronization comes at a price )
  • Scalar substitution : Scalars are data types that can't be decomposed in , Like raw data types and reference type . And the amount of polymerization is decomposable , such as Java Objects in the . Scalar substitution if an object is not externally accessed , And objects can be broken up , This object may not be created when it is actually executed , Instead, it directly creates several of its member variables used by this method . This way not only allows the member variables of the object to be allocated and read and written on the stack , It can also create conditions for further optimization means .

from jdk1.6 Start default on :

Turn on :  -XX:+DoEscapeAnalysis

close :  -XX:-DoEscapeAnalysis

Lock coarsening :

If the virtual machine detects such a string of piecemeal operations that lock the same object , It will extend the scope of lock synchronization to the outside of the whole operation sequence , So it only needs to be locked once

3.1.Synchronized And ReentrantLock The difference between

ReentrantLock =   One AQS synchronizer ( Maintain synchronization status ) + One AQS Synchronous queue + Multiple Condition Waiting in line

3.2 ReentrantLock Inheritance system class diagram

ReentrantLock#lock() Method sequence diagram

Most of the above is transferred from https://blog.csdn.net/u010648018/article/details/79750608

4 Distributed lock

At present, almost many large-scale websites and applications are distributed deployment , Data consistency in distributed scenarios is always an important topic . A distributed CAP Theory tells us “ No distributed system can satisfy the consistency at the same time (Consistency)、 Usability (Availability) And partition fault tolerance (Partition tolerance), At most two items can be satisfied at the same time .” therefore , Many systems have to choose between the three at the beginning of design . In most scenarios in the Internet field , All need to sacrifice strong consistency for high availability of the system , Systems often only need to guarantee “ Final consistency ”, As long as the final time is within the acceptable range of users .

In many scenarios , In order to ensure the final consistency of data , A lot of technical solutions are needed to support , For example, distributed transaction 、 Distributed locks, etc . sometimes , We need to ensure that a method can only be executed by the same thread at the same time . In a stand-alone environment ,Java In fact, it provides a lot of concurrent processing related API, But these API There's nothing you can do in a distributed scenario . That is to say, pure JavaApi It doesn't provide the ability of distributed locks . So there are many schemes for the implementation of distributed locks

Distributed lock should have

1、 In the distributed system environment , A method can only be executed by one thread of one machine at the same time ;
2、 Highly available access lock and release lock ;
3、 High performance acquire lock and release lock ;
4、 Reentrant feature ;
5、 Lock failure mechanism , Prevent deadlock ;
6、 Non blocking lock feature , That is to say, if the lock is not acquired, it will directly return to the acquisition failure

For the implementation of distributed lock , At present, the following schemes are commonly used :

 Implementation of distributed lock based on Database 
Cache based (Redis,memcached,tair) Implement distributed locks
be based on Zookeeper Implement distributed locks

Refer to A simple introduction to distributed lock and three implementation methods

java It's enough to read this article (redis Different ways to implement distributed locks )

About redis Open source distributed lock redisson Principle introduction reference

redisson Distributed lock implementation

redis Distributed lock renewal

java More related articles about the arrangement of lock knowledge in

  1. java In the lock -- Biased locking 、 Lightweight lock 、 spinlocks 、 Heavyweight lock ( Reprint )

    I did a test before , See this article for details < Multithreading +1 Operation of several ways to achieve , And efficiency comparison >, At that time, I was very confused about the test results , I've done it many times , It turns out that the results are the same : 1. Single thread synchronized Highest efficiency ...

  2. java In the lock -- Biased locking 、 Lightweight lock 、 spinlocks 、 Heavyweight lock

    I did a test before , See this article for details < Multithreading +1 Operation of several ways to achieve , And efficiency comparison >, At that time, I was very confused about the test results , I've done it many times , It turns out that the results are the same : 1. Single thread synchronized Highest efficiency ...

  3. In depth understanding of Java In the lock

    Reprint :https://www.jianshu.com/p/2eb5ad8da4dc Java In the lock Common locks are synchronized.volatile. Biased locking . Lightweight lock . Heavyweight lock 1.synchro ...

  4. Java Concurrent programming :Java Lock and thread synchronization mechanism in

    The basics of locks The type of lock Locks are classified on a macro level , There are only two kinds : Pessimistic lock and optimistic lock . Optimism lock Optimistic lock is an optimistic thought , That is to say, read more and write less , The possibility of concurrent writing is low , Every time I go to get the data, I think other people won't modify it , So it won't lock , But it's updating ...

  5. A piece of blog Take you to understand java In the lock

    Preface Recently, I'm reviewing the lock , Yes java The lock in , This article introduces all kinds of locks , I hope to help you . Java Lock of Optimism lock Optimistic lock is an optimistic thought , That is to say, read more and write less , The possibility of concurrent writing is low , Every time I go to get the data, I think others ...

  6. In depth introduction Java In the lock [ principle 、 Lock the optimization 、CAS、AQS]

    1. Why use locks ? lock - To solve the dirty read caused by concurrent operations . Data inconsistency . 2. The basic principle of lock implementation 2.1.volatile Java Programming language allows threads to access shared variables , To ensure that shared variables are updated accurately and consistently , ...

  7. The first 76 section :Java The basic knowledge of

    The first 76 section :Java The basic knowledge of Set up the environment , Installing the operating system , Install backup , It's a mirror image ,jdk Configuration environment ,eclipse Download and unzip to use , download tomcat Discount Commission dynamic agent to solve the problem of website character set coding Use request. ...

  8. To explore the Java In the lock

    One . The function and comparison of lock 1.Lock Interface and its class diagram Lock Interface : yes Java Provides a way to control multiple threads to access shared resources . ReentrantLock:Lock Implementation class of , Provides reentrant locking semantics ReadWrit ...

  9. Java In the lock

    Java Lock classification in In reading many concurrent articles , All kinds of locks will be mentioned, such as fair locks , Optimistic lock and so on , This article introduces the classification of various locks . The introduction is as follows : Fair lock / Not fair lock Reentrant lock Exclusive lock / Shared lock The mutex / Read-write lock Optimism lock / Pessimistic locking branch ...

Random recommendation

  1. Click on .box Follow Click .box.box1

    <!DOCTYPE html> <html> <head> <meta charset='UTF-8'> <title>click</ ...

  2. MVC5+EF6 Getting started complete tutorial

    MVC5+EF6 Getting started complete tutorial 11-- Elaborate MVC Application of storage mode in MVC5+EF6 Getting started complete tutorial 10: Many to many association table update & Use native SQL@20150521 MVC5+EF6 Getting started complete tutorial 9: Multiple tables ...

  3. Python Introduction to reptiles

    Python About reptiles ( From Wikipedia ): Web crawlers start with a unified resource address called seed (URLs) list . When a web crawler accesses these uniform resource locators , They sort out all the hyperlinks on the page , And write them into a " Waiting list ", namely ...

  4. With 16 It prints out a block of memory buff

    The following code ( Support windows And Linux) Will [16 Base number ][ Each row 16 byte ] Print out the contents of a block of memory : void PrintBuffer(void* pBuff, unsigned int nLen) { ...

  5. Could not open INSTALL.LOG file

    Today is the day to unload MailEnable This prompt will pop up when you go to work , use 360 The problem of unloading such tools remains , It's been a long time UNWISE Could not open INSTALL.LOG file I have obsessive-compulsive disorder , Although this software ...

  6. How to C Language adds an object system

    by C Language addition OO The ability to try from the last century 70 It's never stopped until now , Except for the successful ones C++/Objective-C outside , There are many other success stories , such as GTK stay libg An object system is implemented in , And a few years ago OOC, ...

  7. java Object Class learning

    /* * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETA ...

  8. Javascript To achieve the effect of typing

    Sad At the end of the semester Today is the last day of the semester , Finish the last math , Tomorrow we are going to plan our own summer vacation , So we went out to play basketball before we went out , Played roller skating , Tired but happy , The biggest feeling is sadness , Next year I might ...

  9. OC Implementation of the singleton pattern

    SingleClass.m #import <Foundation/Foundation.h> @class SingleClass; static SingleClass *instan ...

  10. Yes java Medium equals() Summary of methods

    Java Summary of the basic study of --equals Method One , It is equal to the introduction of the method 1.1. Master the usage of equal through the following examples 1 package cn.galc.test; 2 3 public class TestEqual ...