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/01 Report--
This article mainly explains "where to use CAS in JAVA". The content in 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 "where to use CAS in JAVA".
What is CAS?
The full name of CAS is compareAndSet, which means comparing and swapping. CAS is a lock-free operation (actually, it is a CPU-level lock, which has nothing to do with the operating system, and a CPU-level lock is faster).
As the name implies, the CAS operation is divided into two steps, first comparing and then swapping. Since you want to make a comparison and then exchange it, it must involve three parameters: your own V, who you compare E with, and who U you need to modify after the comparison.
V is the parameter to be compared, E is the latest value of the variable in memory, and U is the value that needs to be modified. After receiving the three parameters, CAS compares V and E first. If CAS proves that the variable V has not been modified by other threads, CAS will replace V with U; for example, V! = E proves that the variable has been modified by other threads, so CVS believes that this operation is an invalid operation and will not do anything.
/ / JDK version is jdk1.8.0_161 public static void main (String [] args) {/ / 1 AtomicInteger atomicInteger = new AtomicInteger (10); / / 2 boolean flag = atomicInteger.compareAndSet (10,11); System.out.println (flag); / / true / / 3 flag = atomicInteger.compareAndSet (10,11); System.out.println (flag); / / false}
In the above code, the first step is to define an atomic integer type atomicInteger with a value of 10 The second step is to CAS the value. Because AtomicInteger encapsulates the CAS, V is always the value inside the AtomicInteger, so the CAS of the second step lacks the parameter V. CAS compares the value of the atomicInteger variable with the first parameter passed in. Through comparison, it is found that the condition of value = 10 is valid, so the second parameter 11 is replaced with the original 10 (the whole second step is atomic operation). And returns a successful operation true of type boolean In the third step, CAS compares its value 11 (because it has been replaced with 11 in the second step) with the first parameter 10, and finds that the condition of 11! = 10 is not true, so it does not take any action and returns an operation failure false of type boolean.
Where is coming CAS used in JAVA?
At present, CAS is mainly used in jdk in Atomic related classes under J.U.C related packages, such as AtomicInteger, AtomicLong, AtomicBoolean, AtomicDouble, AtomicReference, AtomicReferenceFieldUpdater and so on.
Take getAndUpdate under the AtomicInteger class as an example:
/ * * Atomically updates the current value with the results of * applying the given function, returning the previous value. The * function should be side-effect-free, since it may be re-applied * when attempted updates fail due to contention among threads. * * @ param updateFunction a side-effect-free function * @ return the previous value * @ since 1.8 * / public final int getAndUpdate (IntUnaryOperator updateFunction) {int prev, next; do {prev = get (); next = updateFunction.applyAsInt (prev);} while (! compareAndSet (prev, next)); return prev;} / * * Gets the current value. * * @ return the current value * / public final int get () {return value;}
Step 1: first get the value of the current object through the get () method.
Step 2: updateFunction is a function reference, but its operation is also to assign values and return the expected results.
Step 3: perform the CAS operation, and compare the value obtained in the first step get () with the value in the current object. If the same is the same, change it to next, and continue the loop until the CAS operation returns true.
Step 4: return the currently obtained value prev.
Thank you for your reading, these are the contents of "where to use CAS in JAVA". After the study of this article, I believe you have a deeper understanding of where to use CAS in JAVA, 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.