1

A fan who has worked for 3 years sent me a private message and encountered such a problem during the interview.

"Please tell me about the implementation principle of ReentrantLock", he said a few bits and pieces according to his own understanding at that time.

But it doesn't seem to mention the key point, let me give an issue to talk about the answer.

Well, on this question, let's take a look at the answers of ordinary people and experts.

Ordinary people:

An implementation principle of ReentrantLock, which is a reentrant lock and then a reentrant exclusive lock.

It will solve the problem that when we access some shared resources in parallel with multiple threads, I can lock it through ReentrantLock.

The implementation principle is to realize the synchronization of a thread called a thread through AQS, and its core is AQS.

Expert:

Ok, interviewer, I will answer this question from these aspects.

  • What is ReentrantLock
  • Features of ReentrantLock
  • Implementation principle of ReentrantLock

First of all, ReentrantLock is a reentrant exclusive lock, which is mainly used to solve the problem of multi-thread competition for shared resources.

It has several core features:

  1. It supports reentrancy, that is, when the thread that acquired the lock competes for the same lock again before releasing the lock, it can directly access it without locking.
  2. It supports fair and unfair features
  3. It provides two methods of blocking competing locks and non-blocking competing locks, namely lock() and tryLock().

Then, the underlying implementation of ReentrantLock has several very critical technologies.

  1. Lock competition, ReentrantLock is achieved through the use of CAS mechanism through mutex variables.
  2. Threads that do not compete for locks are stored using a queue synchronizer such as AbstractQueuedSynchronizer, and the bottom layer is implemented through a doubly linked list. When the lock is released, the next thread waiting for the lock will be awakened from the head in the AQS queue.
  3. The characteristics of fairness and unfairness are mainly reflected in whether it is necessary to judge whether there are waiting threads in the AQS queue when competing for locks.
  4. Finally, regarding the reentrant feature of locks, there is a member variable in AQS to save the thread that currently obtains the lock. When the same thread competes for the lock next time, it will not go to the logic of lock competition, but directly increase the Number of reentries.

The above is my understanding of the problem.

image-20220422000218345

Summarize

This question is very simple, but to answer it well, there are two key points.

  1. You must understand the whole design idea of ReentrantLock
  2. The expression must be clear and organized

Again, although basic, it is very important. The depth of the foundation determines the height of the floor.

If you have any interview questions, career development questions, or study questions, you can PM me.

file

Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source for Mic带你学架构 !
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!

跟着Mic学架构
810 声望1.1k 粉丝

《Spring Cloud Alibaba 微服务原理与实战》、《Java并发编程深度理解及实战》作者。 咕泡教育联合创始人,12年开发架构经验,对分布式微服务、高并发领域有非常丰富的实战经验。