The difference between the ReentrantLock and synchronized locks in the "I want to enter the factory" series

In the big factory interviews, especially in the first round of technical interviews, locks are a very high frequency topic. There are mainly two ways in the JDK: ReentrantLock and synchronized keywords in JUC (JAVA Concurrency Framework).

So what is the difference between these two? How to choose in actual work?

ReentrantLock implements synchronized semantics and provides multi-threaded synchronization blocking semantics , that is, a synchronized lock in our usual sense , but the use of ReentrantLock is more flexible and provides functional features that the synchronized keyword does not have.

1. Reentrancy

Both ReentrantLock and synchronized allow reentrancy.

Use a piece of code to illustrate the reentrant semantics of the lock:
Insert picture description here
that is, after a thread has acquired a lock, it can continue to apply for acquisition.

2. ReentrantLock supports fair/unfair locks

Insert picture description here
NonFairSync non-fair lock: first try to preempt the lock when applying for the lock , if the lock is not successfully acquired, it will be helpless to enter the queue.

FairSync Fair Lock: When applying for a lock, it will enter the lock queue and wait for the lock properly.

For example, there are currently 3 threads waiting for a lock. When the thread holding the lock releases the lock after executing the code in the critical section, a new thread also needs to acquire the lock. If it is a fair lock, it needs to wait for the first 3 The thread can acquire the lock after acquiring the lock . If it is unfair lock , he tries to line up with the queue lock total lock other threads together to compete, it is possible to directly acquire the lock on the implementation, this may not seem so unfair that have been queued in the queue thread.

Under normal circumstances, unfair locks involve less thread switching overhead, and the overall performance is higher than fair locks .

3. ReentrantLock supports lock waiting timeout

ReentrantLock supports setting a timeout when applying for a lock, that is, how long it will be automatically awakened when the lock is not successfully obtained, which can be achieved through the tryLock method.
Insert picture description here
This method will return immediately after acquiring the lock, otherwise it will block until it times out. When it returns true, it means that the lock has been successfully acquired.

4. ReentrantLock supports interruption

ReentrantLock provides the lockInterruptibly() method to achieve interruptible lock application , that is, after a thread is locked in a blocked state after competing for a lock, another thread can interrupt the thread, thereby giving up the lock application.

Its realization is through the park() method of LockSupport.
Insert picture description here
The park method of LockSupport can respond to interruption. If the thread is interrupted, the method can stop blocking and be returned, but it will not throw InterruptedException .

5. ReentrantLock supports flexible monitoring

Insert picture description here
ReentrantLock provides many methods, which are of great help to the development of monitoring lock-related applications.

6. Summary

ReentrantLock and synchronized have the same synchronization blocking semantics. Since jdk1.6, the performance of the two is almost the same. If there is no special use requirement, synchronized can be used directly, because the internal storage structure of the lock is taken care of by the virtual machine and is simple to use.


Seeing the words as it appears, I am Weige, a graduate from an ordinary second college, and I have never been exposed to distributed, microservices , high concurrency to achieve workplace transformation through technology sharing , and grow into an excellent evangelist in the RocketMQ community and a senior architect of a large factory. Teacher, published the book "RocketMQ Technology Insider", recorded my growth history in CSDN, welcome everyone to pay attention, private messages, and communicate and progress together.

Share the author's hard-core RocketMQ e-book: How to
Insert picture description here
obtain it: Search [Middleware Interest Circle] on WeChat and reply to RMQPDF to get it.

Guess you like

Origin blog.csdn.net/prestigeding/article/details/115271726