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

相关文章

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

python常见环境管理工具超全解析

《python常见环境管理工具超全解析》在Python开发中,管理多个项目及其依赖项通常是一个挑战,下面:本文主要介绍python常见环境管理工具的相关资料,文中通过代码介绍的非常详细,需要的朋友... 目录1. conda2. pip3. uvuv 工具自动创建和管理环境的特点4. setup.py5.

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

Python包管理工具核心指令uvx举例详细解析

《Python包管理工具核心指令uvx举例详细解析》:本文主要介绍Python包管理工具核心指令uvx的相关资料,uvx是uv工具链中用于临时运行Python命令行工具的高效执行器,依托Rust实... 目录一、uvx 的定位与核心功能二、uvx 的典型应用场景三、uvx 与传统工具对比四、uvx 的技术实

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

Redis过期删除机制与内存淘汰策略的解析指南

《Redis过期删除机制与内存淘汰策略的解析指南》在使用Redis构建缓存系统时,很多开发者只设置了EXPIRE但却忽略了背后Redis的过期删除机制与内存淘汰策略,下面小编就来和大家详细介绍一下... 目录1、简述2、Redis http://www.chinasem.cn的过期删除策略(Key Expir

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解

深入解析 Java Future 类及代码示例

《深入解析JavaFuture类及代码示例》JavaFuture是java.util.concurrent包中用于表示异步计算结果的核心接口,下面给大家介绍JavaFuture类及实例代码,感兴... 目录一、Future 类概述二、核心工作机制代码示例执行流程2. 状态机模型3. 核心方法解析行为总结:三