Java's common lock classification and its characteristics


Optimistic lock | pessimistic lock

Pessimistic locks and optimistic locks are distinguished according to the resource allocation strategy under concurrent conditions

Optimistic locking : I believe that concurrent operations on the same data will not be modified. It will not be locked during the read operation, and it will be judged whether the current data has been modified during the write operation.

Optimistic locking implementation: version number mechanism and CAS mechanism (CAS mechanism needs to solve the ABA problem).

Pessimistic lock : think that for the concurrent operation of the same data, there must be a modification, even if there is no modification, it will be considered a modification. That is, the worst will always happen.

The pessimistic lock always locks the resource or data when holding the data, so that other threads will block when they want to request this resource until the pessimistic lock releases the resource.

Optimistic locking is suitable for reading more and writing less; pessimistic locking is suitable for writing more and reading less.


Fair lock | unfair lock

Fair locks and unfair locks are distinguished according to the order in which the locks are acquired

Fair lock : refers to the allocation of locks in the order in which threads apply for locks under multithreading. First come, first served.

Unfair lock : refers to that the order of acquiring locks under multi-threading is not according to the order of applying for locks. It is possible that the later applied thread will acquire the lock first than the first applied thread.

Because fair locks require additional resources to record the thread's request sequence, unfair locks perform better than fair locks


Exclusive lock | Shared lock

Exclusive locks and shared locks are distinguished according to the number of threads that can hold the lock

Exclusive lock : This lock can only be held by one thread at a time.

Shared lock : The lock can be held by multiple threads.

Exclusive locks and shared locks are implemented through AQS, through different methods to achieve exclusive or shared.


Mutual Exclusion Lock | Read-Write Lock

Mutually exclusive locks and read-write locks are actually specific terms for exclusive locks and shared locks.

The implementation of mutex lock in Java is ReentrantLock, the essence of read-write lock in Java is ReadWriteLock


Bias lock | Lightweight lock | Heavyweight lock

These three locks refer to the state of the lock Synchronized

Biased lock : refers to a piece of synchronization code that has been accessed by a thread, then the thread will automatically acquire the lock, reducing the cost of acquiring the lock.

Lightweight lock : when the lock is biased, accessed by another thread, the biased lock will be upgraded to a lightweight lock, and other threads will acquire the lock through the common sense of the form of spin, will not block, and improve performance .

Heavyweight lock : when the lock is a lightweight lock, although the other thread is spinning, the single spin does not continue indefinitely. When the self-selection reaches a certain number of times, the lock will not be acquired, and it will enter a block The lock expands into a heavyweight lock. Heavyweight locks can cause other application threads to block and degrade performance.


Others: spin lock | reentrant lock | sectional lock

Spin lock : refers to the thread that tries to acquire the lock does not block immediately, but tries to acquire the lock in a circular manner

The advantage of spin lock is to reduce the consumption of thread context switching, the disadvantage is that the cycle will consume CPU.

Reentrant locks : also known as recursive locks. It means that when the same thread acquires the lock in the outer method, it will automatically acquire the lock when entering the inner method, and reentrant lock can avoid deadlock to a certain extent. Java, Synchronizedand ReentrantLockare reentrant lock.

Segmented lock : It is a kind of lock design, not a specific kind of lock. Java in ConcurrentHashMapthe Segmentsegment lock-use design.

Published 7 original articles · won 3 · views 310

Guess you like

Origin blog.csdn.net/sl285720967/article/details/105540504