In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the difference between Synchronized and ReentrantLock lock". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the difference between Synchronized and ReentrantLock lock".
Preface
A few days ago, I came across an IBM blog about the differences between Synchronized,ReentrantLock locks and related performance comparisons. I found that I benefited a lot from reading. I didn't know much about this piece of knowledge before. So this article is a summary of this. Here, the author will mainly discuss the following topics: the difference between Synchronized and ReentrantLock locks, the performance comparison between them, and the lock selection in specific scenarios (that is, the comparison of their advantages and disadvantages).
Similarities and differences between Synchronized Lock and ReentrantLock Lock
Synchronized keyword locks and ReentrantLock locks are common in multithreaded programming, especially the former. So what's the difference between the two? To sum up, there are the following points:
The Synchronized lock comes with a release lock, so there is no need for the user to perform the release lock operation, while the ReentrantLock needs to perform the lock,unlock operation.
ReentrantLock supports more lock primitive operations, such as lock acquisition, lock interrupt waiting, and so on. These more advanced properties are not available on Synchronized.
In terms of performance, ReentrantLock locks will show better performance under the condition of high lock competition, compared with Synchronized locks.
However, some of the properties of the two locks are the same, for example, the default is unfair scheduling policy. In other words, the same multithread performs the request lock operation, and the end result is not the FIFO order to acquire the lock. In fact, under the condition of multi-concurrency competition, fair strategy is not necessarily necessary, in order to maintain this sequence, the system overhead still exists, so the Synchronized lock and the default ReentrantLock are unfair strategy.
Applicable scenarios for Synchronized and ReentrantLock locks
Since the previous section mentioned that ReentrantLock has better performance in highly competitive conditions, and it also supports more advanced properties, does this mean that we will always use ReentrantLock locks regardless of Synchronized locks?
Actually, you can't say that. In the final analysis: when you really want to use the properties of which lock, then use that kind of lock. For example, if you want to use an operation that supports lock interrupts, use ReentrantLock locks. Also, it is not to say that ReentrantLock performance will be better than Synchronized, just blindly use ReentrantLock to replace existing Synchronized locks, only you can really prove that in the current scenario, Synchronized keyword locks have lock competition performance problems, and then replace them. But in most common cases, Synchronized and ReentrantLock locks show about the same performance.
On the other hand, Synchronized lock also has its natural advantages, which is well known and used by more developers. And it is relatively simple and convenient to use, for example, it does not need users to release locks, sometimes novice users forget to perform unlock operations when using similar ReentrantLock locks, resulting in a variety of strange problems in the final program, and it is difficult to locate. All in all, ReentrantLock is a more "advanced" lock, which is more suitable for "advanced" use.
Thank you for your reading, the above is the content of "the difference between Synchronized and ReentrantLock locks". After the study of this article, I believe you have a deeper understanding of the difference between Synchronized and ReentrantLock locks, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.