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

Source code analysis of Java atomic operation class

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

Share

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

This article mainly introduces "Java atomic operation class source code analysis". In daily operation, I believe that many people have doubts about Java atomic operation class source code analysis. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Java atomic operation class source code analysis". Next, please follow the editor to study!

Let's first take a look at some of the source code:

Public class AtomicLong extends Number implements java.io.Serializable {private static final long serialVersionUID = 1927816293512124184L; / 1. Get the Unsafe class instance private static final Unsafe unsafe = Unsafe.getUnsafe (); / / 2. The offset of storing value private static final long valueOffset; static final boolean VM_SUPPORTS_LONG_CAS = VMSupportsCS8 (); / / 3. Used to determine whether Long type unlocked CAS private static native boolean VMSupportsCS8 () is supported; static {try {/ / 4. Get the offset of value in AtomicLong valueOffset = unsafe.objectFieldOffset (AtomicLong.class.getDeclaredField ("value"));} catch (Exception ex) {throw new Error (ex);}} / / 5. The actual variable value private volatile long value; / * Creates a new AtomicLong with the given initial value. * * @ param initialValue the initial value * / public AtomicLong (long initialValue) {value = initialValue;} omit part of the code}

In the above code, an instance of the Unsafe class is obtained through Unsafe.getUnsafe () in code 1 (because the AtomicLong class is under the rt.jar package, and the AtomicLong class is loaded through the Bootstarp class loader). At code 5, value is declared as a volatile type, ensuring memory visibility. Get the offset of the value variable in the AtomicLong class through code 2pj4.

Let's take a look at the main functions in AtomicLong:

Increasing and decreasing code

/ / call unsafe method, after setting value=value+1, return the original value public final long getAndIncrement () {return unsafe.getAndAddLong (this, valueOffset, 1L);} / / call unsafe method, after setting value=value-1, return original value public final long getAndDecrement () {return unsafe.getAndAddLong (this, valueOffset,-1L);} / / call unsafe method, after setting value=value+1, return the incremented value public final long incrementAndGet () {return unsafe.getAndAddLong (this, valueOffset, 1L) + 1L } / / call the unsafe method. After setting value=value-1, the decreasing value public final long decrementAndGet () {return unsafe.getAndAddLong (this, valueOffset,-1L)-1L;} is returned.

The above four functions are operated internally by calling the getAndAddLong method of Unsafe, which is an atomic operation, where the first parameter is referenced by the AtomicLong instance, the second parameter is the offset value of the value variable in AtomicLong, and the third parameter is the value of the second variable to be set.

Where the getAndIncrement () method implements the logic in JDK7 as follows:

Public final long getAndIncrement () {while (true) {long current = get (); long next = current + 1; if (compareAndSet (current, next)) return current;}}

As in the above code, each thread first gets the current value of the variable (because value is a volatile variable, so this is the latest value), then increments it by 1 in working memory, and then uses CAS to modify the value of the variable. If the setting fails, the loop continues to try until the setting is successful.

The logic in JDK8 is:

Public final long getAndIncrement () {retrturn unsafe.getAndAddLong (this, valueOffset, 1L);}

The code of unsafe.getAndAddLong in JDK8 is:

Public final long getAndAddLong (Object var1, long var2, long var4) {long var6; do {var6 = this.getLongVolatile (var1, var2);} while (! this.compareAndSwapLong (var1, var2, var6, var6 + var4); return var6;}

As you can see, the circular logic in AtomicLong in JDK7 has been built into the atomic manipulation class Unsafe in JDK8.

Boolean compareAndSet (long expect,long update)

Public final boolean compareAndSet (long expect,long update) {return unsafe.compareAndSwapLong (this, valueOffset, expect, update);}

The function calls the unsafe.compareAndSwapLong method internally. If the value value in the atomic variable is equal to expect, the value is updated with the update value and true is returned, otherwise false is returned.

At this point, the study of "Java atomic operation class source code analysis" 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

Development

Wechat

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

12
Report