当前位置:网站首页>【juc学习之路第5天】引用原子类和属性修改器
【juc学习之路第5天】引用原子类和属性修改器
2022-06-11 23:51:00 【birdyson】
引用原子类
包含三种:
AtomicReference: 引用类型原子类。实现原理:CAS。
compareAndSet():无法通过匿名对象进行比较,因为比较的内容是地址而不是Obejct.equals()。
AtomicStampedReference: 带有引用版本号的原子类。实现原理:CAS同时C版本号。
compareAndSet():比较内容地址的同时对版本号做出比较。
AtomicMarkableReference: 带有标记的引用原子类。实现原理:CAS同时C标记。
compareAndSet():比较内容地址的同时对标记做出比较。
public class Main {
public static void main(String[] args) throws Exception {
Person birdy = new Person("birdy");
AtomicReference<Person> atomicRef = new AtomicReference<>(birdy);
atomicRef.compareAndSet(birdy, new Person("alexander")); // success
atomicRef.compareAndSet(new Person("birdy"), new Person("alexander")); // false
AtomicStampedReference<Person> stampedRef = new AtomicStampedReference<>(birdy, 1);
stampedRef.compareAndSet(birdy, new Person("alexander"), 1, 2);
AtomicMarkableReference<Person> markRef = new AtomicMarkableReference<>(birdy, true);
markRef.compareAndSet(birdy, new Person("alexander"), true, false);
}
}
解决的问题
AtomicStampedReference和AtomicMarkableReference共同解决多线程下访问数据操作ABA不同步的问题(即:两个线程并发操作,由于更新不同步导致更新错误。对应应用:Github代码提交)

属性修改器
在类中需要原子地get/set某个属性,可以使用属性修改器。包含三种:
AtomicIntegerFiledUpdater: 原子整型成员修改器AtomicLongFieldUpdater: 原子长整型成员修改器AtomicReferenceFieldUpdater: 原子引用成员修改器
针对AtomicLongFieldUpdater分析,它的构造是protected的,它有一个入口静态方法:newUpdater()
newUpdater()
@CallerSensitive
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);
else
return new LockedUpdater<U>(tclass, fieldName, caller);
}
发现有两种更新器:
CASUpdater: 通过CAS机制实现更新。LockedUpdater: 通过锁定两块长整型地址修改属性,用于x32环境。
使用方法
class Person {
private String name;
private volatile long balance; // 必须使用volatile定义Updater操作的成员
private static final AtomicLongFieldUpdater<Person> UPDATER =
AtomicLongFieldUpdater.newUpdater(Person.class, "balance");
public long getBalance() {
return UPDATER.get(this);
}
public void setBalance(long balance) {
UPDATER.set(this, balance);
}
边栏推荐
- Mingdeyang FPGA development board xilinx-k7 core board kinex7k325 410t industrial grade
- 11. conditional statement if, switch
- (greedy + longest ascending subsequence) acwing 896 Longest ascending subsequence II
- Notes on knowledge points of dynamic planning
- HMS core shows the latest open capabilities in mwc2022, helping developers build high-quality applications
- [tense] 1. General present tense 2. Do not translate Chinese
- Delete the receiving address [project mall]
- 2022 safety officer-a certificate test question simulation test platform operation
- swiper
- Lake Shore - supertran VP continuous flow cryogenic thermostat system
猜你喜欢

Solr之基础讲解入门

Two ways of using reuqests in RF

Introduction aux bases de SOLR

(counting class +dp) acwing 900 Integer partition

(digital statistics dp+good) acwing 338 Counting problem

Integrate工具之Jenkins

Lake Shore—SuperTran 连续流低温恒温器系统

Acwing's first question solution attracted the first fan!!! Happy~~~

A new product with advanced product power, the new third generation Roewe rx5 blind subscription is opened

Remix localization, loading local contract file, local link Remix
随机推荐
(dp+ group backpack) acwing 9 Group knapsack problem
2022 safety officer-a certificate test question simulation test platform operation
【BBC learningenglish】with Tango
Teach you to play with SSM framework
NFS quotas:Cannot register service: RPC: Authentication error
El select drop-down box style combined with El table (pseudo) combined with drop-down selection
(dp+ longest common subsequence) acwing 897 Longest common subsequence
04 automatic learning rate - learning notes - lihongyi's in-depth learning 2021
机器学习之数据处理与可视化【鸢尾花数据分类|特征属性比较】
How to completely modify the user name in win10 system and win11 system
解决IDEA下载插件慢的问题
Top selling commodities 【 project mall 】
SAP SD create / modify price list
HMS core shows the latest open capabilities in mwc2022, helping developers build high-quality applications
Flex flexible layout tutorial and understanding of the main axis cross axis: Grammar
Lake Shore - supertran continuous flow cryogenic thermostat system
Live broadcast preview | featurestore meetup V3 is coming!
将数组分成和相等的三个部分[问题分析]
Read the 23 steps of the enterprise through the financial report
思科私有动态路由协议:EIGRP