【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

相关文章

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

HTTP 与 SpringBoot 参数提交与接收协议方式

《HTTP与SpringBoot参数提交与接收协议方式》HTTP参数提交方式包括URL查询、表单、JSON/XML、路径变量、头部、Cookie、GraphQL、WebSocket和SSE,依据... 目录HTTP 协议支持多种参数提交方式,主要取决于请求方法(Method)和内容类型(Content-Ty

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

Spring 依赖注入与循环依赖总结

《Spring依赖注入与循环依赖总结》这篇文章给大家介绍Spring依赖注入与循环依赖总结篇,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Spring 三级缓存解决循环依赖1. 创建UserService原始对象2. 将原始对象包装成工

Java中如何正确的停掉线程

《Java中如何正确的停掉线程》Java通过interrupt()通知线程停止而非强制,确保线程自主处理中断,避免数据损坏,线程池的shutdown()等待任务完成,shutdownNow()强制中断... 目录为什么不强制停止为什么 Java 不提供强制停止线程的能力呢?如何用interrupt停止线程s

SpringBoot请求参数传递与接收示例详解

《SpringBoot请求参数传递与接收示例详解》本文给大家介绍SpringBoot请求参数传递与接收示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录I. 基础参数传递i.查询参数(Query Parameters)ii.路径参数(Path Va

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

Java MCP 的鉴权深度解析

《JavaMCP的鉴权深度解析》文章介绍JavaMCP鉴权的实现方式,指出客户端可通过queryString、header或env传递鉴权信息,服务器端支持工具单独鉴权、过滤器集中鉴权及启动时鉴权... 目录一、MCP Client 侧(负责传递,比较简单)(1)常见的 mcpServers json 配置

GSON框架下将百度天气JSON数据转JavaBean

《GSON框架下将百度天气JSON数据转JavaBean》这篇文章主要为大家详细介绍了如何在GSON框架下实现将百度天气JSON数据转JavaBean,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言一、百度天气jsON1、请求参数2、返回参数3、属性映射二、GSON属性映射实战1、类对象映