Vernacular talk about the issue of Java concurrency interview to talk about your understanding of the AQS?

This article is reproduced from the architecture of notes huperzine

I, EDITORIAL

 

Previous article chatted java concurrency in principle commonly used in atomic Java classes and optimization 8 For details, see the article: "vernacular talk about Java Java interview questions of how to optimize CAS 8 concurrent performance? " .

 

This article, let's talk about the interview are more a question of mass destruction: talk about your understanding of the AQS?

 

There are students before feedback, go to the Internet company for an interview, when the interviewer asked to talk to the concurrency problem. At that time students who suffer heart estimated ten thousand points of damage. . .

 

Because first of all, a lot of people really do not even know what AQS, you may have never heard of. AQS or some people have heard of the term, but may not even know how to spell the full name specific.

 

What is more, might say: AQS? Is not an idea? We usually develop how to use AQS?

 

Overall, many students are estimated to have a feeling foggy AQS, what if the search engine and look for AQS that? Look at a few articles, it is estimated directly gave up because of dense text, really do not understand!

 

Therefore, based on the pain point, let this article, it is with N multiple hand drawing with the most simple vernacular, to tell you what in the end is clear AQS? When you allow students to interview asked this question, and will not know what to do.

 

 

Two, ReentrantLock and AQS relationship

 

 

First we look at, and if java contract under ReentrantLock to lock and release the lock, is what kind of feeling?

 

This basic learned java students should be able, after all this is complicated by the use of basic java API, should everyone is learned, so we direct look at the code like:

 

 

 

 

Above that code should not be difficult to understand it, is nothing more than engage in a Lock object, and then lock and release the lock.

 

Then you might ask, what's this relationship with AQS? Relations go big! Because in a lot of java and API contract are based AQS to achieve locking and releasing locks and other functions, AQS is the base class of java and contract.

 

For example, say ReentrantLock, ReentrantReadWriteLock are achieved based on the underlying AQS.

 

So what is the full name of AQS is it? AbstractQueuedSynchronizer, abstract queue synchronizer . For everyone to draw a diagram to look at the relationship between ReentrantLock and AQS.

 

 

We look at the above chart. To put it plainly, the interior contains a ReentrantLock AQS object is an object AbstractQueuedSynchronizer type. This object is the AQS ReentrantLock can achieve a critical core component of the lock and release the lock.

 

 

Three, ReentrantLock lock and release the lock of the underlying principles

 

Okay, so now if there is a thread tries to lock up with a ReentrantLock () method lock, what will happen then?

 

Very simple, the AQS internal object has a variable called core State , is of type int it represents the locked state . In the initial state, the state value is 0.

 

In addition, the AQS inside there is a critical variable , used to record the current which thread is locked , initialized state, this variable is null.

 

 

Thread 1 then calls the lock ReentrantLock ran () method attempts to lock, the locking process, is to use directly the CAS operation state value from 0 to 1.

 

If you do not know what is CAS, see the article, "vernacular talk about Java Java concurrency 8 interview questions of how to optimize the performance of CAS? "

 

If you do not add people had before the lock, then the value of state is certainly 0, then the thread 1 can be locked successfully.

 

Once locked thread 1 is successful, you can set the current thread is locked themselves. So we look at the chart below, the thread is 1 ran a process locked.

 

 

 

In fact, we see here, we should have a so-called AQS feeling. To put it plainly, is a core component of concurrent bag, there are state variables, locking the core of things such as thread variables, maintaining the locked state.

 

你会发现,ReentrantLock这种东西只是一个外层的API,内核中的锁机制实现都是依赖AQS组件的

 

这个ReentrantLock之所以用Reentrant打头,意思就是他是一个可重入锁。

 

可重入锁的意思,就是你可以对一个ReentrantLock对象多次执行lock()加锁和unlock()释放锁,也就是可以对一个锁加多次,叫做可重入加锁。

 

大家看明白了那个state变量之后,就知道了如何进行可重入加锁!

 

其实每次线程1可重入加锁一次,会判断一下当前加锁线程就是自己,那么他自己就可以可重入多次加锁,每次加锁就是把state的值给累加1,别的没啥变化。

 

 

接着,如果线程1加锁了之后,线程2跑过来加锁会怎么样呢?

 

我们来看看锁的互斥是如何实现的?线程2跑过来一下看到,哎呀!state的值不是0啊?所以CAS操作将state从0变为1的过程会失败,因为state的值当前为1,说明已经有人加锁了!

 

接着线程2会看一下,是不是自己之前加的锁啊?当然不是了,“加锁线程”这个变量明确记录了是线程1占用了这个锁,所以线程2此时就是加锁失败。

 

给大家来一张图,一起来感受一下这个过程:

 

 

 

 

接着,线程2会将自己放入AQS中的一个等待队列,因为自己尝试加锁失败了,此时就要将自己放入队列中来等待,等待线程1释放锁之后,自己就可以重新尝试加锁了

 

所以大家可以看到,AQS是如此的核心!AQS内部还有一个等待队列,专门放那些加锁失败的线程!

 

同样,给大家来一张图,一起感受一下:

 

 

 

接着,线程1在执行完自己的业务逻辑代码之后,就会释放锁!他释放锁的过程非常的简单,就是将AQS内的state变量的值递减1,如果state值为0,则彻底释放锁,会将“加锁线程”变量也设置为null!

 

整个过程,参见下图:

 

 



接下来,会从等待队列的队头唤醒线程2重新尝试加锁。

 

好!线程2现在就重新尝试加锁,这时还是用CAS操作将state从0变为1,此时就会成功,成功之后代表加锁成功,就会将state设置为1。

 

此外,还要把“加锁线程”设置为线程2自己,同时线程2自己就从等待队列中出队了。

 

最后再来一张图,大家来看看这个过程。 

 

 

 

 

 

四、总结

 

OK,本文到这里为止,基本借着ReentrantLock的加锁和释放锁的过程,给大家讲清楚了其底层依赖的AQS的核心原理。

 

基本上大家把这篇文章看懂,以后再也不会担心面试的时候被问到:谈谈你对AQS的理解这种问题了。

 

其实一句话总结AQS就是一个并发包的基础组件,用来实现各种锁,各种同步组件的。它包含了state变量、加锁线程、等待队列等并发中的核心组件。

Guess you like

Origin www.cnblogs.com/alimayun/p/12129816.html