Unfair lock of Java concurrency_ Power node Java college sorting

Obtain unfair lock (based on jdk1.7.0_40)

The process of obtaining locks is the same for unfair locks and fair locks; Their differences are mainly reflected in "different mechanisms for trying to obtain locks". To put it simply, "fair lock" adopts a fair policy (sorting and waiting according to the waiting queue) every time it attempts to obtain a lock; The "unfair lock" adopts an unfair strategy every time it attempts to obtain a lock (ignoring the waiting queue, directly trying to obtain a lock. If the lock is idle, it can obtain the state, and then obtain the lock).

1. lock()

explain:

Lock() will first judge whether the "lock" is idle through compareandset (0,1). If yes, the "current thread" directly obtains the "lock"; Otherwise, call acquire (1) to acquire the lock.

(01) compareandsetstate() is a CAS function, which is used to compare and set the state of the current lock. If the lock status value is 0, set the lock status value to 1.

(02) setexclusiveownerthread (thread. Currentthread()) is used to set "current thread" as the holder of "lock".

Comparison of lock() between "fair lock" and "unfair lock"

2. acquire()

Acquire() is implemented in AQS. Its source code is as follows:

(01) "current thread" first attempts to acquire the lock through tryacquire(). If successful, return directly; If the attempt fails, enter the waiting queue, sort in turn, and then obtain the lock.

(02) if the "current thread" attempt fails, the "current thread" will be added to the end of the "CLH queue (non blocking FIFO queue)" through addwaiter (node. Exclusive).

(03) then, call acquireQueued () to get the lock. In acquirequeueueueueued(), the current thread will wait for all its previous threads in the "CLH queue" to execute and release the lock before it can acquire the lock and return. If the "current thread" is interrupted during sleep waiting, call selfinterrupt() to generate an interrupt by itself.

Comparison between "fair lock" and "unfair lock" about acquire()

For fair locks and unfair locks, only the implementation of tryacquire() function is different; That is, they try to acquire locks by different mechanisms. This is what we call "the difference between their lock acquisition strategies"!

Tryacquire() of unfair lock is in reentrantlock It is implemented in the nonfairsync class of Java. The source code is as follows:

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

explain:

According to the code, we can analyze that the function of tryacquire () is to try to obtain the lock.

(01) if the "lock" is not owned by any thread, set the "lock" status to acquire through the CAS function, set "current thread" as the lock holder, and then return true.

(02) if the "lock" holder is already the current thread, the lock status will be updated.

(03) if the above two cases are not, the attempt is considered to have failed.

Comparison between "fair lock" and "unfair lock" about tryacquire(). Fair lock and unfair lock try to acquire lock in different ways.

When a fair lock attempts to acquire a lock, even if the "lock" is not held by any thread lock, it will judge whether it is the header of the CLH waiting queue; If so, get the lock.

Non fair lock when trying to acquire a lock, if the "lock" is not held by any thread, it will acquire the lock directly no matter where it is in the CLH queue.

Release unfair lock (based on jdk1.7.0_40)

Unfair locks and fair locks have the same methods and strategies for releasing locks.

summary

The difference between fair lock and unfair lock is in the mechanism of obtaining lock. The performance is that when trying to obtain the lock - fair lock, the lock is obtained only when the current thread is the header of the CLH waiting queue; Instead of a fair lock, as long as the current lock is idle, the lock is obtained directly, regardless of the order in the CLH waiting queue.

Only when a non fair lock fails to acquire a lock will it enter the CLH waiting queue like a fair lock.

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
分享
二维码
< <上一篇
下一篇>>