This is the 9th day of my participation in the August Wen Challenge.More challenges in August

preface

Java Lock, fair Lock is not fair Lock, ReentrantLock can not ReentrantLock, read and write Lock and so on a lot of concepts, this article mainly combined with JDK1.8 ReentrantLock source code, talk about the concept of these locks understanding.

1. The Lock framework

1.1 already

1.1.1 Implementation principles of ReentrantLock

ReentrantLock is a concurrency control class implemented based on the AQS concurrency framework. ReentrantLock internally implements three classes,Sync inheriting from AQS, FairSync and NonfairSync inheriting from Sync, implementing various methods of obtaining locks.

Basic implementation:

  1. First try to obtain the lock through CAS
  2. If a thread already owns the lock, it joins the CLH queue and suspends
  3. When the lock is released, the thread at the head of the CLH queue is woken up and the CAS acquires the lock again.

The basic use

Analyzing lock source code is calledsync.lock();

As you can see from the ReentrantLock constructor, an unfair lock is invoked by default. Down below is the crucial first step in which CAS attempts to acquire the lock

1. TryAcquire (not fair) 2. Join the blocking queue.

If CAS fails to acquire the lock, it will be false. If CAS fails to acquire the lock, it will be false. If CAS fails to acquire the lock, it will determine whether the current lock is the current thread.

Look at the difference between fair lock and fair lock

In already, for fairness and justice is based on the definition of extension of the synchronous AbstractQueuedSynchronizer implement, which is on the realization of tryAcquire made semantic control.

1.1.2 fair lock

With a fair lock, the thread will enter the queue directly.

1.1.3 Unfair Locking

An unfair lock, a thread will try to jump the queue first, syncronized is an unfair lock

1.1.4 Reentrant lock

The so-called re-entrant lock refers to the unit of thread. When one thread acquires the object lock, the thread can acquire the lock on the object again, while other threads cannot. Synchronized and ReentrantLock are reentrant locks.

The point of reentrant locks is to prevent deadlocks.

1.1.5 No reentrant lock

The so-called non-reentrant lock means that if the current thread has already acquired the lock by executing a method, attempts to acquire the lock again in the method will not be blocked.

Put these four locks together, in fact, RenntrantLock source code tryAcquire and tryRelease methods implement these four locks. Many blogs also have real life examples like pumping water from Wells that are easy to visualize. Take a look at the reference blog post for details.

1.2 read-write lock

Write lock (exclusive lock, exclusive lock)Is that the lock can only be held by one thread at a time.

If thread T holds an exclusive lock on data A, no other thread can hold any type of lock on data A. The thread that acquires the write lock can both read and modify data.

A read lock (shared lock) means that the lock can be held by multiple threads. If thread T adds A shared lock to data A, other threads can only add A shared lock to data A, not an exclusive lock. The thread that acquires the read lock can only read data, not modify it.

Realize the principle of

State field (int type, 32 bits) in AQS, where state describes the number of read locks and the number of write locks respectively. Therefore, the state variable “bitwise cut” is divided into two parts: the high 16 bits represent the read lock status (number of read locks) and the low 16 bits represent the write lock status (number of write locks).

Reference documentation

AQS – ReadWriteLock source code analysis