JAVA并发-AtomicReference

AtomicReference类提供了对对象引用变量的原子读和写, 原子性意味着多个线程试图更改同一个AtomicReference(例如,通过CAS操作)不会使AtomicReference最终处于不一致的状态。AtomicReference有一个高级的compareAndSet()方法,它允许您将引用与预期值(reference)进行比较,如果它们相等,则在AtomicReference对象内设置一个新的引用/

创建 AtomicReference

下面是创建AtomicReference实例:

AtomicReference atomicReference = new AtomicReference();

如果需要创建指定初始值引用的AtomicReference,可以如下操作:

String initialReference = "the initially referenced string";
AtomicReference atomicReference = new AtomicReference(initialReference);

 

创建一个有类型的AtomicReference

可以通过泛型创建指定类型的 AtomicReference,下面是代码:

AtomicReference<String> atomicStringReference = new AtomicReference<String>();

 

同样可以创建指定初始值的泛型AtomicReference,下面是代码:

String initialReference = "the initially referenced string";
AtomicReference<String> atomicStringReference =  new AtomicReference<String>(initialReference);

 

获取AtomicReference的引用

可以通过AtomicReference  get()方法获取AtomicReference 的存储值,如果不是泛型的AtomicReference 将返回一个Object对象,如果给定了泛型,则返回泛型类型的对象,下面是例子:

AtomicReference atomicReference = new AtomicReference("first value referenced");

String reference = (String) atomicReference.get();

 

注意,有必要将get()返回的引用强制转换为字符串,因为get()在AtomicReference为非类型化时返回对象引用,下面是有类型的例子:

AtomicReference<String> atomicReference = new AtomicReference<String>("first value referenced");

String reference = atomicReference.get();

 

请注意,不再需要强制转换get()返回的引用,因为编译器知道它将返回一个字符串引用。

设置AtomicReference引用

可以通过 AtomicReferenceset()方法,设置AtomicReference的值,在没有泛型的AtomicReference实例中设置的是Object引用作为参数。在设置了泛型的 AtomicReference set() 方法,设置的是定义AtomicReference时设置的泛型,

下面是 AtomicReference set()例子:

AtomicReference atomicReference = new AtomicReference();
    
atomicReference.set("New object referenced");

 

set()方法的有泛型和没泛型没有什么区别,唯一的不同是编译时严格了参数的类型。

CAS操作 AtomicReference引用

AtomicReference类有一个非常有用的方法 compareAndSet(),如果期望值与当前值相等( equals()或者==),那么AtomicReference 将被设置成新的值,如果compareAndSet()设置新的引用后返回true,说明设置成功,否则返回false ,下面是AtomicReference compareAndSet()例子:

String initialReference = "initial value referenced";

AtomicReference<String> atomicStringReference =
    new AtomicReference<String>(initialReference);

String newReference = "new value referenced";
boolean exchanged = atomicStringReference.compareAndSet(initialReference, newReference);
System.out.println("exchanged: " + exchanged);

exchanged = atomicStringReference.compareAndSet(initialReference, newReference);
System.out.println("exchanged: " + exchanged);

 

上面例子创建了一个有类型并且传入初始值的AtomicReference,然后调用两次 comparesAndSet()方法,两次都和初始的引用值比较,第一次因为比较值相等,所以设置了AtomicReference 的新值,第二次还是和初始比较,因为值的不相等,所有调用compareAndSet ()返回false.

参考:http://tutorials.jenkov.com/java-util-concurrent/atomicreference.html

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值