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--
Editor to share with you the principle of Java concurrent CAS and ABA problem example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!
CAS principle
In computer science, comparison and Exchange (Compare And Swap) is an atomic instruction used to achieve multi-thread synchronization. It compares the contents of the memory location with the given value, and only in the same case, modifies the contents of the memory location to the new given value. This is done as a single atomic operation. Atomicity guarantees that the new value is calculated based on the latest information; if the value is updated by another thread at the same time, the write will fail. The result of the operation must indicate whether to replace it; this can be done through a simple Boolean response (this variant is often referred to as comparison and setting) or by returning a value read from the memory location (extracted from Wikipedia)
CAS process
Take AtomicInteger.addAndGet () as an example to explain CAS
Javadoc
Public final int addAndGet (int delta)
Atomically adds the given value to the current value, with memory effects as specified by VarHandle.getAndAdd (java.lang.Object …) .
Parameters:
Delta-the value to add
Returns:
The updated value
In the source code of Java
Public final int addAndGet (int delta) {return U.getAndAddInt (this, VALUE, delta) + delta;}
The VALUE here is obtained during the initialization of this class. Understand that this is when we call the objectFieldOffset of unsafe to obtain the offset of value from the Atomic class file, then VALUE actually records the offset of value.
VALUE is exactly the offset of the value field from the starting memory address of the object AtomicInteger. Since the lowest level of this method is the method that JNI calls native, you need to pass this value.
Private static final long VALUE = U.objectFieldOffset (AtomicInteger.class, "value")
Continue to execute:
/ * Atomically adds the given value to the current value of a field * or array element within the given object {@ code o} * at the given {@ code offset}. * @ param o object/array to update the field/element in * @ param offset field/element offset * @ param delta the value to add * @ since 1.8 * * / @ HotSpotIntrinsicCandidatepublic final int getAndAddInt (Object o, long offset, int delta) {int v; do {v = getIntVolatile (o, offset);} while (! weakCompareAndSetInt (o, offset, v, v + delta)); return v } @ HotSpotIntrinsicCandidatepublic final boolean weakCompareAndSetInt (Object o, long offset, int expected, int x) {return compareAndSetInt (o, offset, expected, x);}
If the value and expect in the obj are equal, it proves that no other thread has changed the variable, then update it to update, and if the CAS in this step is not successful, continue the CAS operation in a spin manner. It looks like two steps from the code, but it is actually done with the help of a CPU instruction in JNI, which is actually an atomic operation.
ABA problem
The cause of ABA problem
CAS needs to check whether the lower value has changed when manipulating the value, and update it if it does not change, but if a value is originally A, becomes B, and becomes An again, then when using CAS to check, you will find that its value has not changed, but it has actually changed. This is CAS's ABA problem.
How to avoid the ABA problem
The common method is to add the version number when updating the data, and use the version number to control the update.
These are all the contents of the article "CAS principles of Java concurrency and sample Analysis of ABA problems". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.