Introduction to the

Synchronization series, which is the name of Tongg who has been thinking about for a long time, was originally intended to write lock-related content. However, CountDownLatch, Semaphore and CyclicBarrier classes in Java do not belong to locks, and they have many things in common with locks. They are all synchronizers for collaborative multi-threaded execution. That’s where the name sync comes from, and that’s where the sync series comes from.

An overview of

This article will cover three topics: Locks in Java, synchronizers and distributed locks.

(1) volatile

(2) the synchronized

(3) AQS and Condition

(4) already

(5) ReentrantReadWriteLock

(6) StampedLock

(7) CountDownLatch

(8) Semaphore

(9) CyclicBarrier

Phaser (10)

(11) Mysql implements distributed locks

(12) Redis implements distributed lock

Zookeeper implements distributed locks

These contents are difficult to understand, and there are many materials on the Internet, but they are often not thoroughly explained. Tong Brother will try to explain these problems clearly in easy to understand language.

Noun explanation

There are also a lot of nouns about lock. Tong Ge has roughly sorted them out and listed them all here:

(1) Fair lock/unfair lock

Fair locks are acquired in the order that threads apply for them.

Unfair lock, it is to point to is not according to the order that the thread applies for to obtain the lock, it is possible that the thread that applies after obtains the lock first instead, if the thread that comes first obtains the lock all the time, can cause lock hunger phenomenon.

In ReentrantLock, you can use the constructor to specify whether the ReentrantLock is a fair lock. By default, the ReentrantLock is an unfair lock, which has the advantage of high throughput.

Synchronized cannot be specified as a fair lock and is always an unfair lock.

(2) Reentrant lock

Reentrant lock means that a thread will automatically acquire the lock if it tries to acquire the lock again. The advantage of reentrant lock is to avoid deadlock.

ReentrantLock and synchronized are both reentrant locks.

(3) Exclusive lock/shared lock

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

A shared lock means that the lock can be held by multiple threads at a time.

ReentrantLock and synchronized are both exclusive locks. ReadWriteLock’s read lock is shared and its write lock is exclusive.

(4) Mutex/read/write lock

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

ReentrantLock and synchronized are mutually exclusive

ReadWriteLock is a read/write lock

(5) Optimistic lock/pessimistic lock

Pessimistic locking refers to the belief that concurrent operations on the same data will inevitably change, even if no change will occur, so it must be locked.

Optimistic locking refers to the belief that concurrent operations on the same data may not be modified. When updating data, it tries to update data and keeps trying if it fails.

Pessimistic lock applies to the scenario with many write operations, optimistic lock applies to the scenario with many read operations.

(6) segmental lock

Segment-based lock is a kind of lock design idea that reduces the granularity of locks. It is mainly used in ConcurrentHashMap to achieve efficient concurrent operations. When the operation does not need to update the entire array, only one item in the array is locked.

(7) Bias lock/lightweight lock/heavyweight lock

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

Biased locking means that a piece of synchronized code is always accessed by a thread, so the thread will automatically acquire the lock, reducing the cost of acquiring the lock.

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

Heavyweight lock refers to a lightweight lock. When the spinning thread spins for a certain number of times and has not acquired the lock, it will enter the blocking state. This lock is upgraded to heavyweight lock, which will block other threads and reduce the performance.

(8) Spin lock

Spin locking, which means that the thread trying to acquire the lock does not block, but keeps trying in a circular manner. This has the advantage of reducing the context switch of the thread to unlock the lock, improving performance, but the disadvantage is that the loop consumes CPU.

(9) Monitor lock

Synchronized is implemented using monitorenter and Monitorexit.

(10) Mutex lock

The mutex, locksupport.part () underlying is implemented through mutex.

eggs

Recruit to:

Because tong Elder brother is busy with his work, it is difficult to do daily work, so we sincerely invite all friends to contribute actively, we can learn and make progress together.

You can leave a message “contribute” to me in the background of the public number, and discuss the content of the contribution in detail with your friends.

Of course, other problems can also be in the public number backstage message, whether it is life, work, psychological or physical, welcome the intrusive, message will be back.


Welcome to pay attention to my public number “Tong Elder brother read source code”, view more source code series articles, with Tong elder brother tour the ocean of source code.