AtomicLong、AtomicLongArray、AtomicLongFieldUpdater 深入源码解析

本文主要是介绍AtomicLong、AtomicLongArray、AtomicLongFieldUpdater 深入源码解析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  本篇主要对AtomicLong、AtomicLongArray、AtomicLongFieldUpdater进行讲解,本博已经对AtomicInteger、AtomicIntegerArray、AtomicIntegerFieldUpdater源码进行了深入解析。其实这几个类原理都是类似的,不同的是针对的操作数据类型有差异。

  演示示例:

  可以参考《AtomicBoolean 完全源码解析》、《AtomicInteger 完全源码解析》、《AtomicIntegerArray 深入源码解析》、《AtomicIntegerFieldUpdater 深入源码解析》序列文章。

  首先,新建一个Thread类,用于模拟多线程环境:

package com.securitit.serialize.atomics;import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongArray;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;public class AtomicLongThread extends Thread {// AtomicLong实例.private AtomicLong atomicLong;// AtomicLongArray实例.private AtomicLongArray atomicLongArray;// AtomicLongFieldUpdater实例.private AtomicLongFieldUpdater<AtomicLongFieldUpdaterBean> atomicLongFieldUpdater;// AtomicLongFieldUpdaterBean实例.private AtomicLongFieldUpdaterBean atomicLongFieldUpdaterBean;// CountDownLatch可以进行线程计数,到达指定计数时,可唤醒调用await方法的线程.private CountDownLatch countDownLatch;public AtomicLongThread(AtomicLong atomicLong, AtomicLongArray atomicLongArray,AtomicLongFieldUpdater<AtomicLongFieldUpdaterBean> atomicLongFieldUpdater,AtomicLongFieldUpdaterBean atomicLongFieldUpdaterBean, CountDownLatch countDownLatch) {this.atomicLong = atomicLong;this.atomicLongArray = atomicLongArray;this.atomicLongFieldUpdater = atomicLongFieldUpdater;this.atomicLongFieldUpdaterBean = atomicLongFieldUpdaterBean;this.countDownLatch = countDownLatch;}@Overridepublic void run() {// 对AtomicLong变量进行累加.atomicLong.incrementAndGet();// 对AtomicLongArray指定索引位置的值进行累加.for (int index = 0; index < atomicLongArray.length(); index++) {atomicLongArray.incrementAndGet(index);}// 对AtomicLongFieldUpdater实例变量进行累加.if(atomicLongFieldUpdater.compareAndSet(atomicLongFieldUpdaterBean, 100L, 200L)) {System.out.println("AtomicLongFieldUpdater 更新成功.");} else {System.out.println("AtomicLongFieldUpdater 更新失败.");}countDownLatch.countDown();}}

  然后,为AtomicLongFieldUpdater测试新建Bean类,类中只包含一个counter属性:

package com.securitit.serialize.atomics;public class AtomicLongFieldUpdaterBean {// 内部计数器.public volatile long counter = 100;public long getCounter() {return counter;}public void setCounter(long counter) {this.counter = counter;}}

  最后,是测试类,在开启多线程操作AtomicLong、AtomicLongArray、AtomicArrayFieldUpdater,以模拟多线程环境:

package com.securitit.serialize.atomics;import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongArray;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;public class AtomicLongTester {// AtomicLong实例.private static AtomicLong atomicLong;// AtomicLongArray实例.private static AtomicLongArray atomicLongArray;// AtomicLongFieldUpdater实例.private static AtomicLongFieldUpdater<AtomicLongFieldUpdaterBean> atomicLongFieldUpdater;// AtomicLongFieldUpdaterBean实例.private static AtomicLongFieldUpdaterBean atomicLongFieldUpdaterBean;// CountDownLatch可以进行线程计数,到达指定计数时,可唤醒调用await方法的线程.private static CountDownLatch countDownLatch = new CountDownLatch(4);public static void main(String[] args) throws Exception {// 初始化AtomicLong实例.atomicLong = new AtomicLong(100L);// 初始化AtomicLongArray实例.atomicLongArray = new AtomicLongArray(10);// 初始化AtomicLongArray实例数组内部值.for (int index = 0; index < atomicLongArray.length(); index++) {// 用当前索引作为值.atomicLongArray.set(index, index);}// 初始化AtomicLongFieldUpdater实例.atomicLongFieldUpdater = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterBean.class, "counter");// 初始化AtomicLongFieldUpdaterBean实例.atomicLongFieldUpdaterBean = new AtomicLongFieldUpdaterBean();atomicLongFieldUpdaterBean.setCounter(100L);// 启动多线程测试.new AtomicLongThread(atomicLong, atomicLongArray, atomicLongFieldUpdater, atomicLongFieldUpdaterBean,countDownLatch).start();new AtomicLongThread(atomicLong, atomicLongArray, atomicLongFieldUpdater, atomicLongFieldUpdaterBean,countDownLatch).start();new AtomicLongThread(atomicLong, atomicLongArray, atomicLongFieldUpdater, atomicLongFieldUpdaterBean,countDownLatch).start();new AtomicLongThread(atomicLong, atomicLongArray, atomicLongFieldUpdater, atomicLongFieldUpdaterBean,countDownLatch).start();// 等待所有线程执行完毕.countDownLatch.await();// 输出AtomicLong的值.System.out.println("AtomicLong:" + atomicLong.get());// 输出AtomicLongArray数值.for (int index = 0; index < atomicLongArray.length(); index++) {if (atomicLongArray.get(index) != index + 4) {System.out.println("数据计算出现问题.");}}}}

  输出结果:

AtomicLongFieldUpdater 更新失败.
AtomicLongFieldUpdater 更新成功.
AtomicLongFieldUpdater 更新失败.
AtomicLongFieldUpdater 更新失败.
AtomicLong:104

  从结果可以看出:

  · AtomicLong在4个线程中被正确增加到4。

  · AtomicLongArray未打印异常日志,说明已更新成功,若想确认,可以将atomicLongArray每一项打印出来确认。

  · AtomicLongFieldUpdater在4个线程中,只有一个线程将值由100L修改为200L,其他线程执行CAS过程中,compare失败。

  源码分析:

  实现基础:

  AtomicLong:

// long类型内存偏移位置.
private static final long valueOffset;
// 记录VM是否支持长整型的无锁CAS操作.
// 虽然Unsafe类的compareAndSwapLong方法在两种情况下均有效.
static final boolean VM_SUPPORTS_LONG_CAS = VMSupportsCS8();
// 数据存储变量.
private volatile long value;

  AtomicLongArray:

// 表示内存中int[]的初始地址.
private static final int base = unsafe.arrayBaseOffset(long[].class);
// int[]数组中元素位移个数.
private static final int shift;
// 用来存储真实int[]数组的变量.
private final long[] array;

  AtomicLongFieldUpdater:

public static <U> AtomicLongFieldUpdater<U> newUpdater(Class<U> tclass, String fieldName){Class<?> caller = Reflection.getCallerClass();if (AtomicLong.VM_SUPPORTS_LONG_CAS)return new CASUpdater<U>(tclass, fieldName, caller);elsereturn new LockedUpdater<U>(tclass, fieldName, caller);
}

  AtomicLongFieldUpdater是一个abstract类,内置了两个静态内部类:CASUpdater和LockedUpdater,若VM支持long类型的CAS操作,则使用CASUpdater进行数据操作;若VM不支持long类型的CAS操作,则使用LockedUpdater(即synchronized)进行数据操作,保证多线程环境下数据安全性。

  基本方法:

  AtomicLong:

// 获取long的值.
public final long get();
// 设置long的值.
public final void set(long newValue);
// 延迟设置long的值.
public final void lazySet(long newValue);
// 先取值,再修改.
public final long getAndSet(long newValue);
// 依赖CAS,进行赋值操作.
public final boolean compareAndSet(long expect, long update);
// 依赖CAS,进行赋值操作.
public final boolean weakCompareAndSet(long expect, long update);
// 先取值,再自增.
public final long getAndIncrement();
// 先取值,再自减.
public final long getAndDecrement();
// 先取值,再原值加上delta.
public final long getAndAdd(long delta);
// 先自增,再取值.  
public final long incrementAndGet();
// 先自减,再取值.
public final long decrementAndGet();
// 先原值加上delta,再取值.
public final long addAndGet(long delta);
// 先取值,再修改当前原值.
public final long getAndUpdate(LongUnaryOperator updateFunction);
// 先修当前原值,再取值.
public final long updateAndGet(LongUnaryOperator updateFunction);
// 使用LongBinaryOperator对当前值和第一个参数进行计算,并更新当前值.
public final long getAndAccumulate(long x, LongBinaryOperator accumulatorFunction);
// 使用LongBinaryOperator对当前值和第一个参数进行计算,并更新当前值.
public final long accumulateAndGet(long x, LongBinaryOperator accumulatorFunction);
// 按int类型取值.
public int intValue();
// 按long类型取值.
public long longValue();
// 按float类型取值.
public float floatValue();
// 按double类型取值.
public double doubleValue();

  AtomicLongArray:

// 数组内容长度.
public final int length();
// 取得指定索引位置的值.
public final long get(int i);
// 设置指定索引位置的值.
public final void set(int i, long newValue);
// 延迟设置指定索引位置的值.
public final void lazySet(int i, long newValue);
// 先取值,再修改.
public final long getAndSet(int i, long newValue);
// 依赖CAS,进行比较替换.
public final boolean compareAndSet(int i, long expect, long update);
// 依赖CAS,进行比较替换.
public final boolean weakCompareAndSet(int i, long expect, long update);
// 先取值,再自增.
public final long getAndIncrement(int i);
// 先取值,再自减.
public final long getAndDecrement(int i);
// 先取值,原值加上delta.
public final long getAndAdd(int i, long delta);
// 先自增,再取值.
public final long incrementAndGet(int i);
// 先自减,再取值.
public final long decrementAndGet(int i);
// 先原值加delta,再取值.
public long addAndGet(int i, long delta);
// 先取值,再修改当前原值.
public final long getAndUpdate(int i, LongUnaryOperator updateFunction);
// 先修当前原值,再取值.
public final long updateAndGet(int i, LongUnaryOperator updateFunction);
// 使用LongBinaryOperator对当前值和第一个参数进行计算,并更新当前值.
public final long getAndAccumulate(int i, long x, LongBinaryOperator accumulatorFunction);
// 使用LongBinaryOperator对当前值和第一个参数进行计算,并更新当前值.
public final long accumulateAndGet(int i, long x, LongBinaryOperator accumulatorFunction);

  AtomicLongFieldUpdater:

// 依赖CAS,进行比较替换.
public abstract boolean compareAndSet(T obj, long expect, long update);
// 依赖CAS,进行比较替换.
public abstract boolean weakCompareAndSet(T obj, long expect, long update);
// 设置值.
public abstract void set(T obj, long newValue);
// 延迟设置值.
public abstract void lazySet(T obj, long newValue);
// 取值.
public abstract long get(T obj);
// 先取值,再修改.
public long getAndSet(T obj, long newValue);
// 先取值,再自增.
public long getAndIncrement(T obj);
// 先取值,再自减.
public long getAndDecrement(T obj);
// 先取值,再原值加上delta.
public long getAndAdd(T obj, long delta);
// 先自增,再取值.
public long incrementAndGet(T obj);
// 先自减,再取值.
public long decrementAndGet(T obj);
// 先原值加上delta,再取值.
public long addAndGet(T obj, long delta);
// 先取值,再修改当前原值.
public final long getAndUpdate(T obj, LongUnaryOperator updateFunction);
// 先修当前原值,再取值.
public final long updateAndGet(T obj, LongUnaryOperator updateFunction);
// 使用LongBinaryOperator对当前值和第一个参数进行计算,并更新当前值.
public final long getAndAccumulate(T obj, long x, LongBinaryOperator accumulatorFunction);
// 使用LongBinaryOperator对当前值和第一个参数进行计算,并更新当前值.
public final long accumulateAndGet(T obj, long x, LongBinaryOperator accumulatorFunction);

  AtomicLong、AtomicLongArray、AtomicLongFieldUpdater使用方式与AtomicInteger、AtomicIntegerArray、AtomicIntegerFieldUpdater基本保持一致,只要搞清楚其中一种类的操作方式,另一个也可以一样使用。

  注:文中源码均来自于JDK1.8版本,不同版本间可能存在差异。

  如果有哪里有不明白或不清楚的内容,欢迎留言哦!

这篇关于AtomicLong、AtomicLongArray、AtomicLongFieldUpdater 深入源码解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/393597

相关文章

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应

MyBatis中$与#的区别解析

《MyBatis中$与#的区别解析》文章浏览阅读314次,点赞4次,收藏6次。MyBatis使用#{}作为参数占位符时,会创建预处理语句(PreparedStatement),并将参数值作为预处理语句... 目录一、介绍二、sql注入风险实例一、介绍#(井号):MyBATis使用#{}作为参数占位符时,会

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

深度解析Java DTO(最新推荐)

《深度解析JavaDTO(最新推荐)》DTO(DataTransferObject)是一种用于在不同层(如Controller层、Service层)之间传输数据的对象设计模式,其核心目的是封装数据,... 目录一、什么是DTO?DTO的核心特点:二、为什么需要DTO?(对比Entity)三、实际应用场景解析

从原理到实战深入理解Java 断言assert

《从原理到实战深入理解Java断言assert》本文深入解析Java断言机制,涵盖语法、工作原理、启用方式及与异常的区别,推荐用于开发阶段的条件检查与状态验证,并强调生产环境应使用参数验证工具类替代... 目录深入理解 Java 断言(assert):从原理到实战引言:为什么需要断言?一、断言基础1.1 语

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

深度解析Python装饰器常见用法与进阶技巧

《深度解析Python装饰器常见用法与进阶技巧》Python装饰器(Decorator)是提升代码可读性与复用性的强大工具,本文将深入解析Python装饰器的原理,常见用法,进阶技巧与最佳实践,希望可... 目录装饰器的基本原理函数装饰器的常见用法带参数的装饰器类装饰器与方法装饰器装饰器的嵌套与组合进阶技巧