Brief introduction to java synchronization opening

  • 2021-07-26 07:42:15
  • OfStack

Overview
The content of this article is a general introduction, which roughly includes three major topics: locks, synchronizers and distributed locks in java. The general contents are as follows:

(1) volatile

(2) synchronized

(3) AQS and Condition

(4) ReentrantLock

(5) ReentrantReadWriteLock

(6) StampedLock

(7) CountDownLatch

(8) Semaphore

(9) CyclicBarrier

(10) Phaser

(11) Mysql implements distributed locking

(12) Redis implements distributed locking

(13) Zookeeper implements distributed locking

These contents are obscure and difficult to understand, and there are many materials on the Internet, but they are often not thorough enough. Brother Tong will try his best to explain these problems clearly in easy-to-understand language.

Noun interpretation
There are also many nouns about locks. Brother Tong roughly sorted them out and listed them all here:

(1) Fair/Unfair Lock

Fair lock refers to acquiring locks in the order of thread application.

Unfair lock is not in accordance with the order of thread application to obtain the lock, it is possible that the thread applying for the lock first. If the first thread 1 can't get the lock directly, it will cause lock starvation.

In ReentrantLock, whether it is a fair lock or not can be specified by the construction method, and it is an unfair lock by default. The advantage of unfair lock is that the throughput is large.

synchronized cannot be specified as a fair lock, 1 is not a fair lock.

(2) Reentrant lock

Reentrant lock means that when a thread acquires a lock and then tries to acquire it, it will automatically acquire the lock. The advantage of reentrant lock is to avoid deadlock.

ReentrantLock and synchronized are both reentrant locks.

(3) Exclusive/shared locks

Exclusive lock means that the lock can only be held by one thread at a time.

Shared lock means that the lock can be held by multiple threads at one time.

ReentrantLock and synchronized are exclusive locks, ReadWriteLock read locks are shared locks, and write locks are exclusive locks.

(4) Mutex/read-write lock

Similar to the concept of exclusive lock/shared lock, it is the concrete implementation of exclusive lock/shared lock.

ReentrantLock and synchronized are mutually exclusive locks

ReadWriteLock is a read-write lock

(5) Optimistic lock/pessimistic lock

Pessimistic lock refers to the belief that the concurrent operation of the same data will inevitably be modified, even if it will not be modified, so 1 must be locked.

Optimistic lock refers to thinking that the concurrent operation of the same data will be modified. When updating the data, try to update the data, and try continuously if it fails.

Pessimistic locks are suitable for scenarios with many writes, while optimistic locks are suitable for scenarios with many reads.

(6) Sectional lock

Segmented lock is a kind of lock design idea, which refines the granularity of lock. It is mainly used in ConcurrentHashMap to realize efficient concurrent operation. When the operation does not need to update the whole array, only one item in the array can be locked.

(7) Bias Lock/Lightweight Lock/Heavyweight Lock

These three locks are primarily optimized for synchronized and are indicated by the object monitor's fields in the object header.

Biased lock means that a piece of synchronization code 1 is directly accessed by a thread, so this thread will automatically acquire the lock, reducing the cost of acquiring the lock.

Lightweight lock means that when the lock is biased lock, it is accessed by another thread, and the biased lock will be upgraded to lightweight lock, which will try to acquire the lock by spinning without blocking and improve performance.

Heavyweight lock means that when the lock is a lightweight lock, when the spinning thread spins for a certain number of times and has not yet acquired the lock, it will enter a blocking state, and the lock will be upgraded to a heavyweight lock, which will block other threads and reduce their performance.

(8) Spin locking

Spin lock means that the thread trying to acquire the lock will not block, but keeps trying in a circular way. This has the advantage of reducing the unlocking caused by context switching of threads and improving performance. The disadvantage is that the loop will consume CPU.

(9) Monitor lock

synchronized implementation, using monitorenter and monitorexit to achieve.

(10) mutex Lock

Mutex, LockSupport. part () The underlying layer is implemented through mutex.


Related articles: