The classification of the lock

In the case of multiple threads, operations on shared variables can cause thread-safety problems. Using mutex is one of the most common strategies, and there are many different locks. We mainly introduce the following common locks:

1. Optimistic and pessimistic locks

1.1 optimistic locking

  • concept

Optimistic locking is a kind of optimistic thinking, when reading data, thinking that others will not modify, so do not lock. Only when writing data, determine whether the current value is the same as the expected value and update it if it is. Update operation to atomic operation.

  • Application scenarios

1.2 pessimistic locks

Pessimistic locking is a pessimistic idea, that there are many concurrent scenarios, and other threads will modify the data every time, so every time when the data will be locked. Therefore, all other threads are blocked until the current thread finishes executing and releases the lock, and other threads can compete for the lock. This ensures that only one thread can enter a critical section at a time.

  • Application scenarios

1.3 summary

Optimistic locks are suitable for read and write scenarios, while pessimistic locks are suitable for read and write scenarios.

Disadvantages of optimistic locking:

  1. ABA problem

Definition: If thread 1 reads data V as A, then another thread 2 also operates on V, changing the value of V to B and then to A. When thread 1 finds that V is still A, it assumes that V has not been modified.

Tom’s girlfriend ran away with Tom and then got back together with Tom. (Poor Joe)

Solution: Use version numbers

  1. Cycle time is expensive

If the CAS does not succeed for a long time, it incurs a significant overhead on the CPU.

  1. Atomic operations of only one shared variable are guaranteed

CAS can be used to guarantee atomic operations when operating on one shared variable, but CAS cannot guarantee atomic operations when operating on multiple shared variables.

2. Fair and unfair locks

2.1 fair lock

Fair locking is the idea that multiple threads acquire locks in the order in which they are applied. In the concurrent environment, each thread will first check the waiting queue maintained by the lock when acquiring the lock. If the queue is empty, the current thread will occupy the lock. If the current waiting queue is not empty, it will join the end of the waiting queue, get the thread from the queue according to the PRINCIPLE of FIFO, and then occupy the lock.

Advantages: Won’t starve to death

Disadvantages: The overall throughput efficiency is lower than that of an unfair lock, all threads in the waiting queue except the first thread will block, and the CPU cost of waking up a blocked thread is higher than that of an unfair lock.

2.2 Unfair lock

Fair locking is the idea that when multiple threads acquire a lock, they will directly attempt to acquire it, and if they cannot obtain it, they will use an unfair lock. Locks are not acquired on a first-come, first-served basis between Dole threads.

Advantages: Higher overall throughput, because the thread can acquire the lock directly without having to wake up all the threads to compete. Cons: Threads waiting in queues can starve to death or wait too long to acquire locks.

3. Reentrant lock

Reentrant locks are the idea that when multiple threads acquire a lock,

4. Shared locks and exclusive locks

4.1 Shared lock

Can be held by multiple threads, holding the lock in a shared manner.

When there are only reads, the lock is shared between multiple reader threads.

4.2 an exclusive lock

Also called exclusive lock, can only be held by a thread, in an exclusive way to hold the lock. Synchronized in the JDK and Lock in JUC are mutexes.

5. Read and write locks

Read/write lock: Read operations use read locks and write operations use write locks. In the absence of write locks, read locks are non-blocking, which improves execution efficiency to a certain extent. That is, only one write operation is allowed at a time, and the read operations do not affect each other. Read/write locks are classified into read locks and write locks. Multiple read locks are not mutually exclusive. Read locks and write locks are mutually exclusive. Read/write locks are implemented in Java as ReentrantReadWriteLock.

6. Block lock

ConcurrentHashMap: The ConcurrentHashMap is divided into multiple segments. There are 16 segments in total. Each operation will locate the segment and then lock the segment instead of the entire HashMap.

Reference article: juejin.cn/post/686792…