ReenTrantLock reentrant lock (difference from synchronized) summary

ReenTrantLock reentrant lock (difference from synchronized) summary

Reentrancy:

From the name, ReenTrantLock literally means a re-entry lock. In fact, the lock used by the synchronized keyword is also re-entrant. There is little difference between the two. Both are that the same thread has not entered once, and the counter of the lock is incremented by 1, so the lock cannot be released until the counter of the lock drops to 0.

 

Implementation of the lock:

Synchronized is implemented by JVM, and ReenTrantLock is implemented by JDK. What is the difference? To put it bluntly, it is similar to the difference between the operating system to control the implementation and the user's own code implementation. The implementation of the former is more difficult to see, and the latter has direct source code for reading.

 

The difference in performance:

Before Synchronized optimization, the performance of synchronized was much worse than that of ReenTrantLock, but since Synchronized introduced biased locks and lightweight locks (spin locks), the performance of the two is almost the same, when both methods are available. Next, the official even recommends the use of synchronized. In fact, the optimization of synchronized is based on the CAS technology in ReenTrantLock. They all try to solve the locking problem in user mode to avoid blocking threads entering kernel mode.

 

Functional difference:

Convenience: It is obvious that the use of Synchronized is more convenient and concise, and the compiler guarantees the lock and release of the lock, while ReenTrantLock needs to be manually declared to lock and release the lock. In order to avoid deadlock caused by forgetting to manually release the lock, it is best to Declare release lock in finally.

Fine-grained and flexible locks: ReenTrantLock is clearly superior to Synchronized

 

ReenTrantLock unique abilities:

1. ReenTrantLock can specify whether it is a fair lock or an unfair lock. And synchronized can only be an unfair lock. The so-called fair lock is that the thread that waits first gets the lock first.

2. ReenTrantLock provides a Condition class, which is used to wake up the threads that need to be woken up in groups, rather than wake up a thread randomly or wake up all threads like synchronized.

3. ReenTrantLock provides a mechanism to interrupt threads waiting for locks, which is implemented by lock.lockInterruptibly().

 

The principle of ReenTrantLock implementation:

I saw the relevant source code analysis on the Internet. Originally this should be the core of this article, but it feels more complicated and will not be explained in detail. In short, the implementation of ReenTrantLock is a kind of spin lock, which is realized by cyclically calling the CAS operation. lock. Its performance is better because it avoids the blocking state that makes the thread enter the kernel state. Trying to avoid the thread entering the blocked state of the kernel is the key to analyzing and understanding the lock design.

 

When to use ReenTrantLock:

The answer is if you need to implement the three unique features of ReenTrantLock.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325555570&siteId=291194637