博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【并发】2、AtomicReferenceFieldUpdater初体验
阅读量:4654 次
发布时间:2019-06-09

本文共 2455 字,大约阅读时间需要 8 分钟。

对于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;    }}

 

效果展示:

 

转载于:https://www.cnblogs.com/cutter-point/p/8481323.html

你可能感兴趣的文章
Socket网络编程(TCP/IP/端口/类)和实例
查看>>
实时系统概念
查看>>
理一理字节对齐的那些事
查看>>
#pragma pack(push,1)与#pragma pack(1)的区别(转)
查看>>
socket、端口、进程的关系
查看>>
SCPI 语言简介
查看>>
TVS瞬态抑制二极管选型指南
查看>>
Excel中文转拼音(完整版)
查看>>
Excel中如何获取汉字拼音首字母
查看>>
Echaer图表——P100080项目
查看>>
回车 登录
查看>>
打开文件,读取TXT
查看>>
打开 导入Excel文件 (异步)
查看>>
播放声音
查看>>
导出Excel 文件
查看>>
combox 绑定数据
查看>>
一键导入Excel 文件 (同步)
查看>>
python 使用tesseract进行图片识别
查看>>
20175305 《信息安全系统设计基础》第1-2周学习总结
查看>>
硬件设计:POE--POE基础
查看>>