In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "A brief introduction of optimistic lock and pessimistic lock in Java". In daily operation, I believe that many people have doubts about the brief introduction of optimistic lock and pessimistic lock in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "optimistic lock and pessimistic lock in Java". Next, please follow the editor to study!
1. Category 1: optimistic lock and pessimistic lock
A) pessimistic locks: locks are added because they think that other threads will interfere with their own thread operations
i. Specific forms: synchronized keyword and lock implementation class
B) optimistic lock: think that no other thread will affect its own thread operation, so do not lock
i. Specific form: the incremental operation of the atomic class of java
ii. Principle: using cas algorithm
C) Cas algorithm: exchange and comparison
i. Three numbers are involved: the memory value V that needs to be read and written, the value A to be compared, and the new value B to be written
ii. Specific operation: if you want V, update V with B, otherwise nothing will be done.
iii. Can use ABA problem: cas algorithm needs to determine whether the memory value V has changed, if a value becomes b value and then changes back to a value, cas algorithm will be unable to judge, resulting in an error. Solve the above problem: add the version number before the variable to change aba into 1a2b3c
iv. Long cycle time and high overhead because spin consumes cpu
v. Only one atomic operation of a shared variable is guaranteed.
2. Category 2
A) reentrant locks: locks that support reentry, exclusive locks
3. Classification 3
A) read-write locks: a pair of locks, read locks, write locks, allowing multiple threads to access at the same time
At this point, the study of "A brief introduction to optimistic locks and pessimistic locks in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.