In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-27 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 the differences between ReentrantLock and Synchronized. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Similarity: thread synchronization is controlled by locking. And it is all blocking synchronization, that is, when a thread acquires the lock of the object and enters the synchronization block, other threads accessing the synchronization block must block and wait outside the synchronization block.
Difference: synchronized is a keyword of the java language, is a native syntax level of mutual exclusion, and needs to be implemented by jvm. ReentrantLock is an API-level mutex provided after Jdk 1.5, which requires lock () and unlock () methods to complete with try/finally statement blocks. Convenience: synchronized is convenient and simple to use, and the lock is locked and released by the compiler. ReentrantLock needs to lock and release locks manually. Officially, because ReetrantLock locks and releases manually, locks are more flexible and fine-grained.
After synchronized optimization, after using bias lock and lightweight lock (spin lock), the performance of both is almost the same, and when both methods are available, it is even recommended to use synchronized.
Synchronized forms two bytecode instructions, monitorenter and monitorexit, before and after the synchronization block. Before executing the monitorenter instruction, you first try to get the lock object. If the object is not locked, or if the current thread already owns that object lock, add 1 to the lock's calculator and, accordingly, subtract the lock's calculator when executing the monitorexit instruction.
This is the end of the article on "what's the difference between ReentrantLock and Synchronized". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please 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.
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.