Interviewer: Tell me about the difference between ReentrantLock and Synchronized

table of Contents

How to use

Method to realize

Fair and unfair

Reentrant lock

Interruptible

Conditional queue

to sum up


Hello everyone! Meet everyone again. In order to avoid the embarrassment of the interview , today I will talk to you about the difference between ReentrantLock and Synchronized in more popular language!

 

How to use


Synchronized can modify instance methods, static methods, and code blocks. The lock is automatically released.

ReentrantLock generally requires a try catch finally statement to acquire the lock in the try and release the lock in the finally. Need to manually release the lock.

 

Method to realize


Synchronized is a heavyweight lock. Heavyweight locks require threads from the kernel mode and user mode back and forth switching . For example: A thread switches to B thread, A thread needs to save the current scene, B thread switching also needs to save the scene. The disadvantage of this is that it consumes system resources .

ReentrantLock is a lightweight lock. Using cas+volatile to manage threads , there is no need for thread switching. The thread acquiring the lock feels that it will definitely succeed. This is an optimistic thought (may fail).

Use an image example to illustrate: For example, when you read my article, you feel that the concept of "heavyweight lock" is not very clear, so immediately go to read other articles about "heavyweight lock", and then go back later. In the following, this behavior is called handover. Saving the scene means that your brain needs to remember the point where you jumped and then continue reading. If the length of the article is large, the more things your brain needs to remember, the more nerves will be consumed. In the same way, in lightweight locks, you feel that the concept of "heavyweight locks" is not very clear. He will not read other articles immediately. He will continue to read it for a while. If you really don't understand, go to the information. Note that: These are two different ways of thinking, the former is passive blocking pessimistic locking, the state is block, the latter is actively blocking optimistic locking, the state is wait.

Fair and unfair


Synchronized only has unfair locks.

ReentrantLock provides both fair and unfair locks, and the default is unfair. Fair lock is passed true through the constructor.

Use an image example to illustrate: queuing for dinner, Synchronized allows to jump in the queue, if ReentrantLock is a fair lock, it is not allowed to jump in the queue.

 

Reentrant lock


Both Synchronized and ReentrantLock are reentrant. Synchronized is a native method implemented in C++, while ReentrantLock is a JUC package implemented in Java.

Use an image example to illustrate: the following picture: a room in a room, there is a lock inside and outside the room, but only the only key can be opened, the person who has the key can enter the entrance 1, and then enter the door 2, where the entrance door 2 is The lock is reentrant.

In ReentrantLock, the number of reentrants is represented by an integer state. Enter 1 time and increase 1 time, come out 1 time and decrease 1 time.

image.png

 

 

Interruptible


Synchronized is uninterruptible.

ReentrantLock provides interruptible and non- interruptible methods. The lockInterruptibly method means interruptible, and the lock method means uninterruptible.

Use an image example to illustrate: Call Lian and call Lian girlfriend to do nucleic acid together, call Lian girlfriend in front, so call Lian girlfriend do it first when they enter the door, call Lian suddenly receive a call from the leader while waiting in line outside the door If you want to go back to fix the bug, Calling Lian now has two options, 1. Don't say hello to your girlfriend, go back to fix the bug immediately, 2. Wait for your girlfriend to finish nucleic acid, go in and say hello to your girlfriend, and then go back to fix the bug. Both of these situations will eventually lead to a result, that the training cannot complete the nucleic acid. In both cases, although the training is interrupted by the leader, the first situation is that the training immediately feedbacks to the leader and can be interrupted, and the second situation In order not to be a single dog, Jia Lian said hello and then went to fix the bug. It should be noted that "hello" needs to obtain the lock in advance, that is, you need to wait for Jia Lian's girlfriend to complete the nucleic acid test. If it were you, what would you do if you encounter such a situation as practicing? Looking forward to your reply! Pay attention, don’t get lost, I’m called Lian [Official Account], I’m calling and practicing.

 

Conditional queue


Synchronized has only one waiting queue.

One lock in ReentrantLock can correspond to multiple condition queues. Expressed by newCondition.

Use an image example to illustrate: the hen laying eggs and the egg pickers correspond to the producers and consumers. After the hen lays eggs, the egg pickers need to be notified by the hen. During the hen laying process, the egg pickers will enter Condition queue (waiting queue). After the egg picker has finished picking up the eggs, the egg picker needs to notify the hen to continue laying eggs. During the egg picking process, the hen also needs to join the conditional queue to wait.

 

 

Note: There are several concepts that need to be explained. Synchronization queue, condition queue and waiting queue.

Synchronous queue : multiple threads compete for a lock failure at the same time and are suspended threads.

Conditional queue : The thread that is executing calls await/wait, and the thread that joins from the synchronization queue will enter the conditional queue. Calling signal/signalAll/notify/notifyAll from the executing thread will add one or more threads of the condition queue to the synchronization queue.

Waiting queue : A concept with conditional queue.

 

 

to sum up


Today we described the relationship between ReentrantLock and Synchronized in easy-to-understand text. Please like and comment if you like it! Pay attention, don’t get lost, I’m called Lian [Official Account], I’m calling and practicing. Looking forward to seeing you next time!

tempimage1611629165941.gif

Guess you like

Origin blog.csdn.net/duyabc/article/details/113175705