Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between Synchronized and ReentrantLock

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail what is the difference between Synchronized and ReentrantLock, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Reentrancy:

In terms of the name, ReenTrantLock literally means a reentrant lock. In fact, the lock used by the synchronized keyword is also reentrant, and there is little difference between the two.

Both are not entered by the same thread once, and the counter of the lock increases by 1, so the lock cannot be released until the counter of the lock falls to 0.

Implementation of the lock:

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

Differences in performance:

Before Synchronized optimization, the performance of synchronized was much worse than that of ReenTrantLock, but since Synchronized introduced bias locks, lightweight locks (spin locks), the performance of the two has been about the same.

When both methods are available, officials even recommend using synchronized. In fact, I feel that the optimization of synchronized draws lessons from the CAS technology in ReenTrantLock. It is an attempt to solve the locking problem in the user mode to avoid thread blocking in the kernel state.

Functional differences:

Convenience: it is obvious that the use of Synchronized is more convenient and simple, and the compiler ensures the locking and release of the lock, while ReenTrantLock needs manual declaration to add the lock and release the lock, in order to avoid forgetting to manually release the lock caused by deadlock, it is best to declare the release lock in finally.

Fine granularity and flexibility of locks: it is clear that ReenTrantLock is better than Synchronized

Unique capabilities of ReenTrantLock:

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

2.ReenTrantLock provides a Condition (conditional) class to wake up threads that need to be woken up in groups, rather than waking up either one thread at random or all threads as synchronized does.

3.ReenTrantLock provides a mechanism for interrupting threads waiting for locks, which is implemented through lock.lockInterruptibly ().

The principle of ReenTrantLock implementation:

See the relevant source code analysis on the Internet, this is supposed to be the core of this article, but feel more complex do not explain in detail, to put it simply, the implementation of ReenTrantLock is a kind of spin lock, through the circular call CAS operation to achieve locking.

Its performance is also better because it avoids the blocking state that puts the thread into the kernel state. Trying to prevent threads from entering the blocking state of the kernel is the key for us to analyze and understand lock design.

About what the difference between Synchronized and ReentrantLock is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report