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

相关文章

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

MySQL 缓存机制与架构解析(最新推荐)

《MySQL缓存机制与架构解析(最新推荐)》本文详细介绍了MySQL的缓存机制和整体架构,包括一级缓存(InnoDBBufferPool)和二级缓存(QueryCache),文章还探讨了SQL... 目录一、mysql缓存机制概述二、MySQL整体架构三、SQL查询执行全流程四、MySQL 8.0为何移除查

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

使用Java实现一个解析CURL脚本小工具

《使用Java实现一个解析CURL脚本小工具》文章介绍了如何使用Java实现一个解析CURL脚本的工具,该工具可以将CURL脚本中的Header解析为KVMap结构,获取URL路径、请求类型,解析UR... 目录使用示例实现原理具体实现CurlParserUtilCurlEntityICurlHandler

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

深入理解Apache Airflow 调度器(最新推荐)

《深入理解ApacheAirflow调度器(最新推荐)》ApacheAirflow调度器是数据管道管理系统的关键组件,负责编排dag中任务的执行,通过理解调度器的角色和工作方式,正确配置调度器,并... 目录什么是Airflow 调度器?Airflow 调度器工作机制配置Airflow调度器调优及优化建议最

Spring IOC控制反转的实现解析

《SpringIOC控制反转的实现解析》:本文主要介绍SpringIOC控制反转的实现,IOC是Spring的核心思想之一,它通过将对象的创建、依赖注入和生命周期管理交给容器来实现解耦,使开发者... 目录1. IOC的基本概念1.1 什么是IOC1.2 IOC与DI的关系2. IOC的设计目标3. IOC

java中的HashSet与 == 和 equals的区别示例解析

《java中的HashSet与==和equals的区别示例解析》HashSet是Java中基于哈希表实现的集合类,特点包括:元素唯一、无序和可包含null,本文给大家介绍java中的HashSe... 目录什么是HashSetHashSet 的主要特点是HashSet 的常用方法hasSet存储为啥是无序的

Go中sync.Once源码的深度讲解

《Go中sync.Once源码的深度讲解》sync.Once是Go语言标准库中的一个同步原语,用于确保某个操作只执行一次,本文将从源码出发为大家详细介绍一下sync.Once的具体使用,x希望对大家有... 目录概念简单示例源码解读总结概念sync.Once是Go语言标准库中的一个同步原语,用于确保某个操