Fair lock of Java concurrency (2)_ Power node Java college sorting

Release fair lock (based on jdk1.7.0_40)

1. unlock()

Unlock() in reentrantlock Implemented in Java, the source code is as follows:

explain:

Unlock() is an unlock function, which is implemented through the release() function of AQS. Here, the meaning of "1" is the same as that of "acquire (1) function for obtaining lock". It is a parameter for setting "lock release status". Because the "fair lock" is reentrant, for the same thread, the lock state is - 1 every time the lock is released.

For AQS, the relationship between reentrantlock and sync is as follows:

From this, we find that sync is reentrantlock Java, and sync is a subclass of AQS.

2. release()

Release() is implemented in AQS. The source code is as follows:

explain:

Release () will first call tryrelease () to try to release the lock held by the current thread lock. If successful, wake up the subsequent waiting thread and return true. Otherwise, false is returned directly.

3. tryRelease()

Tryrelease() is in reentrantlock Java. The source code is as follows:

explain:

Tryrelease() attempts to release the lock. (01) if "current thread" is not "lock holder", an exception will be thrown. (02) if the "current thread" owns the lock 0 after this lock release operation (that is, the current thread completely releases the "lock"), set the "lock" holder to null, that is, the lock is available. At the same time, update the lock status of the current thread to 0. Getstate(), setstate(), which was introduced in the previous chapter, will not be explained here. Getexclusiveownerthread(), setexclusiveownerthread() is in the parent class abstractownablesynchronizer of AQS Java. The source code is as follows:

4. unparkSuccessor()

In release(), if the "current thread" releases the lock successfully, it will wake up the subsequent threads of the current thread. According to the FIFO rules of the CLH queue, "current thread" (that is, the thread that has obtained the lock) must be head; If the CLH queue is not empty, the next waiting thread of the lock will be awakened.

Let's take a look at the source code of unparksuccess (), which is implemented in AQS.

explain:

The function of unparksuccess() is to "wake up the successor thread of the current thread". After the subsequent thread is awakened, it can acquire the lock and resume operation.

About node For the description of waitstatus, please refer to "Introduction to node class in the previous chapter".

summary

The process of "releasing a lock" is simpler than that of "acquiring a lock". When releasing a lock, the main operation is to update the status of the lock corresponding to the current thread. If the current thread has completely released the lock, set the holding thread of the lock to null, set the status of the current thread to null, and then wake up the subsequent thread.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>