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 is JAVA CAS?

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

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what is JAVA CAS". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is JAVA CAS?"

CAS (compare and swap)

CAS is an instruction mechanism supported by CPU. The CAS operation consists of three operands "memory location (V), expected original value (A) and new value (B)". If the value of the memory location matches the expected original value, then the processor will automatically update the location value to the new value, otherwise, the processor will not do anything.

JAVA CAS

Separately speaking, CAS has nothing to do with the application language. By JAVA CAS, we are talking about the atomic operation-related API implemented by JDK based on CAS. For more information, please see java.util.concurrent.atomic.

The so-called CAS "spin" has nothing to do with CPU itself, but is implemented by the application language itself, as follows:

Let's take a look at how the atomic AtomicInteger + + operation of Java performs CAS spin:

Public final int getAndAddInt (Object var1, long var2, int var4) {int var5; do {var5 = this.getIntVolatile (var1, var2); / / before updating, query the old value v5} while (! this.compareAndSwapInt (var1, var2, var5, var5+var4)); / / the new value var5+var4. If the old value of memory is found to be inconsistent during the update, false is returned, and the loop continues to spin. Return var5;}

ABA problem

If a value is queried as A, and then changed to B and A by another thread, you will find that its value has not changed when you check it with CAS, but it has actually changed.

Solution to the ABA problem: append a version number to the variable and add 1 to the version number each time the variable is updated

At this point, I believe you have a deeper understanding of "what is JAVA CAS", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report