对于volatile对象的原子更新

AtomicReferenceFieldUpdater这个对象进行原子更新的时候,外部操作对象只能是public,因为外部访问不到private对象,但是在内内部确可以自己封装一个compareAndSet

package disruptor.test;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

import org.junit.Test;

/**
* 基于反射的实用工具,可以对指定类的指定 volatile 字段进行原子更新。该类用于原子数据结构,该结构中同一节点的几个引用字段都独立受原子更新控制。
* @author xiaof
*
* compareAndSet(T obj, V expect, V update)
* obj - 有条件地设置其字段的对象
* expect - 预期值
* update - 新值
* 如果当前值 == 预期值,则以原子方式将此更新器管理的给定对象的字段设置为给定的更新值。对 compareAndSet 和 set 的其他调用,此方法可以确保原子性,但对于字段中的其他更改则不一定确保原子性
*
*/
public class AtomicReferenceFieldUpdaterTest { @Test
public void test1() {
AtomicReferenceFieldUpdater update = AtomicReferenceFieldUpdater.newUpdater(Temp.class, String.class, "name2");
Temp temp = new Temp();
update.compareAndSet(temp, null, "66");
update.compareAndSet(temp, "661", "6677");
System.out.println(temp.name2);
update.compareAndSet(temp, "66", "6677");
System.out.println(temp.name2); //以下会报错
/*
AtomicReferenceFieldUpdater update2 = AtomicReferenceFieldUpdater.newUpdater(Temp.class, String.class, "name3");
update2.compareAndSet(temp, null, "321");
System.out.println(temp.name3);
update2.compareAndSet(temp, null, "3211");
System.out.println(temp.name3);
update2.compareAndSet(temp, "321", "32112");
System.out.println(temp.name3);
*/ temp.compareAndSetName(null, "cutter");
System.out.println("内部原子操作:" + temp.getName());
temp.compareAndSetName3(null, "point");
System.out.println("内部原子操作:" + temp.getName3()); } } class Temp {
private volatile String name;
public volatile String name2;
protected volatile String name3; public void compareAndSetName(String preValue, String name) {
AtomicReferenceFieldUpdater update = AtomicReferenceFieldUpdater.newUpdater(Temp.class, String.class, "name");
update.compareAndSet(this, preValue, name);
} public void compareAndSetName3(String preValue, String name) {
AtomicReferenceFieldUpdater update = AtomicReferenceFieldUpdater.newUpdater(Temp.class, String.class, "name3");
update.compareAndSet(this, preValue, name);
} public String getName() {
return this.name;
} public String getName3() {
return this.name3;
}
}

效果展示:

最新文章

  1. ASP.NET Core 缓存技术 及 Nginx 缓存配置
  2. 侯捷老师C++大系之C++面向对象开发:(一)不带指针的类:Complex复数类的实现过程
  3. 现场打印智能无线PDA安卓POS 条码识别、打印、数据采集销售开单收银管理软件
  4. 【原创】Kafka console consumer源代码分析(二)
  5. Python-09-线程、进程、协程、异步IO
  6. 初步理解JNDI
  7. 读javascript高级程序设计05-面向对象之创建对象
  8. Date.UTC日期格式
  9. jsp页面
  10. tc 2014 college tour 250 500
  11. macos ssh host配置及免密登陆
  12. Oracle core05_事务和一致性
  13. 编程之美2013 初赛一 A - 竞价 学习大牛的思路
  14. js便签笔记(13)——jsonp事实上非常easy【ajax跨域请求】
  15. openresty使用笔记(一)
  16. IdentityServer(14)- 使用EntityFramework Core配置和操作数据
  17. Java使用Try with resources自动关闭资源
  18. javascript 函数对象
  19. 为什么会出现__pycache__文件夹?
  20. 【转】关于Vue打包的一个要注意的地方

热门文章

  1. opencv知识积累
  2. java常用设计模式十一:策略模式
  3. linux命令详解之useradd命令使用方法[linux下 添加用户、删除用户、修改用户密码、用户组管理]
  4. 以太网MAC地址规范
  5. php 制作二维码 phpqrcode.php
  6. innodb mvcc多版本实现
  7. C语言实现BMP图片生成
  8. day3用户交互,格式化输出,数据类型,流程控制
  9. Ng第十四课:降维(Dimensionality Reduction)
  10. split(),reverse(),join()