Understanding and applicable scenarios of biased locks, lightweight locks, and heavyweight locks

Object header: The lock used by synchronized is stored in the Java object header. The Mark Word in the Java object header stores the HashCode, generational age, and lock flag of the object by default, and is mainly used to indicate the thread lock status of the object.

Stack frame: Also called process activity record, it is a data structure used by the compiler to implement process/function calls.

Locks will gradually escalate with the competition of threads, biased locks => lightweight locks => heavyweight locks. Locks can be upgraded but not downgraded. The purpose of the upgrade is to improve the efficiency of acquiring and releasing locks.

Biased lock
When a thread accesses the synchronization block to acquire the lock, it will store the lock-biased thread ID in the lock record of the object header and the stack frame. Later, when the thread enters and exits the synchronization block, it does not need to perform CAS (comparison and exchange, see below Record it in detail for the second time) to lock and unlock.

Determine whether to use CAS competitive lock by judging whether there is a bias lock pointing to the current thread in the Mark Word of the object header.

Applicable scenarios: Biased locks are only suitable for systems where most of the locks are not contested. If there are a large number of contended locks in the system, it will cause the threads holding the locks to switch continuously. At this time, you can consider disabling biased locks.

Biased locks can be turned off via JVM parameters:

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

Lightweight locks.
Blocking threads requires the CPU to switch from user mode to kernel mode, which is expensive. And it may happen that the lock is released shortly after blocking. In order to reduce performance consumption, lightweight locks are introduced.

lock

Before the thread executes the synchronization block, the JVM will first create a space for storing the lock record in the stack frame of the current thread, and copy the Mark Word in the object header to the lock record, which is officially called Displaced Mark Word. Then the thread tries to use CAS to replace the Mark Word in the object header with a pointer to the lock record. If it succeeds, the current thread acquires the lock. If it fails, it means that other threads compete for the lock, and the current thread tries to use spin to acquire the lock.

unlock

When lightweight unlocking, an atomic CAS operation will be used to replace the Displaced Mark Word back to the object header. If successful, it means that there is no competition. If it fails, it means that the current lock is in competition, and the lock will expand into a heavyweight lock.

Applicable scenario: A small number of threads alternately acquire locks, and the synchronization block executes very fast. Go for response time.

Comparison of advantages and disadvantages
Bias lock

Advantages: locking/unlocking does not require additional consumption

Disadvantage: When there is competition, there will be additional consumption of revoking locks or upgrading locks

lightweight lock

Advantages: Competing threads will not be blocked (using spin), which improves the response speed of the program

Disadvantage: Spinning threads that can never get a lock will consume cpu and cause cpu waste (spinning seems to be a meaningless cycle, you can set a maximum time for spin waiting)

heavyweight lock

Advantages: thread competition does not use spin, thread competition lock will be blocked after failure, cpu consumption will be reduced, and data throughput will be increased

Disadvantage: thread blocking, slow response

Applicable scenario: A large number of threads compete for locks at the same time, pursuing throughput.


The following scenarios can be asynchronized

1. Print log
2. Email sending File upload timing task synchronization data
3. Send configuration
4. Deploy service
5. Call charge recharge
6. Microservice health scan

Guess you like

Origin blog.csdn.net/m1195900241/article/details/126382244