【Java并发】原子类源码分析之AtomicBoolean

2024-08-30 10:32

本文主要是介绍【Java并发】原子类源码分析之AtomicBoolean,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

JDK1.8

public class AtomicBoolean implements java.io.Serializable {private static final long serialVersionUID = 4654671469794556979L;// setup to use Unsafe.compareAndSwapInt for updates// 使用Unsafe.compareAndSwapInt进行更新private static final Unsafe unsafe = Unsafe.getUnsafe();// valueOffset是对象在内存中的偏移量private static final long valueOffset;//初始阶段,通过unsafe来获取AtomicBoolean类的value字段在内存中的偏移量valueOffsetstatic {try {valueOffset = unsafe.objectFieldOffset(AtomicBoolean.class.getDeclaredField("value"));} catch (Exception ex) { throw new Error(ex); }}//value被volatile修饰,保证其内存可见性,后面也会以value来判断AtomicBoolean值private volatile int value;/*** Creates a new {@code AtomicBoolean} with the given initial value.** @param initialValue the initial value*///构造方法:支持设置初始值,并且以int来存储  true -> 1 false -> 0public AtomicBoolean(boolean initialValue) {value = initialValue ? 1 : 0;}/*** Creates a new {@code AtomicBoolean} with initial value {@code false}.*///构造方法:没有赋值,没赋值的话,int默认为0, 0 -> falsepublic AtomicBoolean() {}/*** Returns the current value.** @return the current value*///获取当前value值 final修饰public final boolean get() {return value != 0;}/*** Atomically sets the value to the given updated value* if the current value {@code ==} the expected value.** @param expect the expected value* @param update the new value* @return {@code true} if successful. False return indicates that* the actual value was not equal to the expected value.*///如果当前值 == 期望值expect,便会原子地更新成update值,返回true//如果当前值 != 期望值expect,更新失败,返回falsepublic final boolean compareAndSet(boolean expect, boolean update) {int e = expect ? 1 : 0;int u = update ? 1 : 0;return unsafe.compareAndSwapInt(this, valueOffset, e, u);}/*** Atomically sets the value to the given updated value* if the current value {@code ==} the expected value.** <p><a href="package-summary.html#weakCompareAndSet">May fail* spuriously and does not provide ordering guarantees</a>, so is* only rarely an appropriate alternative to {@code compareAndSet}.** @param expect the expected value* @param update the new value* @return {@code true} if successful*///是的,这里和compareAndSet是一模一样的实现//有兴趣的可以百度下public boolean weakCompareAndSet(boolean expect, boolean update) {int e = expect ? 1 : 0;int u = update ? 1 : 0;return unsafe.compareAndSwapInt(this, valueOffset, e, u);}/*** Unconditionally sets to the given value.** @param newValue the new value*///不带任何条件地进行赋值,虽然value被volatile修饰,但是set这里修改不是原子方法public final void set(boolean newValue) {value = newValue ? 1 : 0;}/*** Eventually sets to the given value.** @param newValue the new value* @since 1.6*///注释的含义是:最终设置成功//和set方法对比来看,lazySet底层调用的是unsafe.putOrderedInt//从hotspot源码看putOrderedXXX源码分析底层操作//可以参考这个同学的分析,https://www.jianshu.com/p/4ed887664b13public final void lazySet(boolean newValue) {int v = newValue ? 1 : 0;unsafe.putOrderedInt(this, valueOffset, v);}/*** Atomically sets to the given value and returns the previous value.** @param newValue the new value* @return the previous value*///原子地更新value值为newValue,并返回更新之前的值//先获取更新前的值,然后while一直尝试更新直到成功为止public final boolean getAndSet(boolean newValue) {boolean prev;do {prev = get();} while (!compareAndSet(prev, newValue));return prev;}/*** Returns the String representation of the current value.* @return the String representation of the current value*/public String toString() {return Boolean.toString(get());}}

 

这篇关于【Java并发】原子类源码分析之AtomicBoolean的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

Spring Boot 中 RestTemplate 的核心用法指南

《SpringBoot中RestTemplate的核心用法指南》本文详细介绍了RestTemplate的使用,包括基础用法、进阶配置技巧、实战案例以及最佳实践建议,通过一个腾讯地图路线规划的案... 目录一、环境准备二、基础用法全解析1. GET 请求的三种姿势2. POST 请求深度实践三、进阶配置技巧1

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

Spring Boot 处理带文件表单的方式汇总

《SpringBoot处理带文件表单的方式汇总》本文详细介绍了六种处理文件上传的方式,包括@RequestParam、@RequestPart、@ModelAttribute、@ModelAttr... 目录方式 1:@RequestParam接收文件后端代码前端代码特点方式 2:@RequestPart接

SpringBoot整合Zuul全过程

《SpringBoot整合Zuul全过程》Zuul网关是微服务架构中的重要组件,具备统一入口、鉴权校验、动态路由等功能,它通过配置文件进行灵活的路由和过滤器设置,支持Hystrix进行容错处理,还提供... 目录Zuul网关的作用Zuul网关的应用1、网关访问方式2、网关依赖注入3、网关启动器4、网关全局变

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

基于SpringBoot实现分布式锁的三种方法

《基于SpringBoot实现分布式锁的三种方法》这篇文章主要为大家详细介绍了基于SpringBoot实现分布式锁的三种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、基于Redis原生命令实现分布式锁1. 基础版Redis分布式锁2. 可重入锁实现二、使用Redisso

SpringBoot的全局异常拦截实践过程

《SpringBoot的全局异常拦截实践过程》SpringBoot中使用@ControllerAdvice和@ExceptionHandler实现全局异常拦截,@RestControllerAdvic... 目录@RestControllerAdvice@ResponseStatus(...)@Except

Springboot配置文件相关语法及读取方式详解

《Springboot配置文件相关语法及读取方式详解》本文主要介绍了SpringBoot中的两种配置文件形式,即.properties文件和.yml/.yaml文件,详细讲解了这两种文件的语法和读取方... 目录配置文件的形式语法1、key-value形式2、数组形式读取方式1、通过@value注解2、通过

Java 接口定义变量的示例代码

《Java接口定义变量的示例代码》文章介绍了Java接口中的变量和方法,接口中的变量必须是publicstaticfinal的,用于定义常量,而方法默认是publicabstract的,必须由实现类... 在 Java 中,接口是一种抽象类型,用于定义类必须实现的方法。接口可以包含常量和方法,但不能包含实例