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 are the problems of CAS algorithm

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what are the problems existing in the CAS algorithm". In the daily operation, I believe that many people have doubts about the problems existing in the CAS algorithm. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for everyone to answer the doubts about "what are the problems with the CAS algorithm?" Next, please follow the editor to study!

CAS (Compare and Swap) algorithm:

1) principle: 1) CAS has three operands, memory value V, expected value (old value) A, and new value B to be written. 2) if and only if the expected value A = memory value V, change the memory value V to the new value B, otherwise do nothing. 3) CAS can be regarded as an optimistic lock. 2) Application: used to manage concurrent access to shared data. 3) problems with CAS: 1) ABA problem: if the value of a variable is originally A, then becomes B, and finally becomes A, then you will find that its value has not changed when you use CAS to check, but it has actually changed. Solution: use the version number. 2) long cycle time and high overhead 3) only one atomic operation of a shared variable can be guaranteed: cyclic CAS can be used to ensure atomicity when operating on a shared variable, but cyclic CAS cannot guarantee atomicity when operating on multiple shared variables. Solution: 1 > use locks. 2 > merge multiple shared variables into a single shared variable to manipulate, that is, encapsulate multiple shared variables into an object, and then use cyclic CAS to manipulate the object. Eg:JDK provides the java.util.concurrent.atomic.AtomicReference class to guarantee atomicity between reference objects. 4) implementation of atomic operations in java: 1) use lock 2) Loop CAS description: except for biased locks, JVM implements locks in a loop CAS, that is, when a thread wants to enter the synchronization block, it uses a loop CAS way to obtain the lock, and when it exits the synchronization block, it uses a loop CAS to release the lock. At this point, the study of "what are the problems with the CAS algorithm" 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.

Share To

Servers

Wechat

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

12
Report