AtomicReference

本文翻译自http://tutorials.jenkov.com/java-util-concurrent/atomicreference.html,人工翻译,仅供学习交流。

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);

创建类型化的原子引用

您可以使用Java泛型来创建一个类型化的AtomicReference,下面是一个类型化的AtomicReference示例:

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

您还可以为类型化的AtomicReference设置初始值。下面是一个带有初始值的类型化AtomicReference实例化示例:

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

获取AtomicReference 引用对象

您可以使用AtomicReference的get()方法获取存储在AtomicReference中的引用。如果你有一个非类型化的AtomicReference,那么get()方法返回一个Object引用。如果你有一个类型化的AtomicReference,当你创建对象时,get()会返回在AtomicReference变量上声明的类型引用。
下面是第一个非类型化的AtomicReference get()示例:

AtomicReference atomicReference = new AtomicReference("first value referenced");
String reference = (String) atomicReference.get();

注意将get()返回的引用必须转换为String类型,因为当AtomicReference是非类型化时,get()返回才是Object引用。下面是一个类型化的AtomicReference示例:

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

注意,不再需要对get()返回的引用进行强制转换,因为编译器知道它将返回一个String引用。

设置AtomicReference Reference

您可以使用AtomicReference实例的set()方法设置存储在该实例中的引用。在非类型化AtomicReference实例中,set()方法将Object引用作为参数。当你声明AtomicReference时,在一个类型化的AtomicReference中,set()方法接受声明为其类型的任何类型作为参数。下面是一个AtomicReference set()的例子:

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

对于非类型化引用或类型化引用,set()方法的使用没有区别。你将经历的唯一真正的不同是编译器将限制您可以在类型化AtomicReference上设置的类型。

比较并设置AtomicReference引用

AtomicReference类包含一个名为compareAndSet()的有用方法。compareAndSet()方法可以比较存储在AtomicReference实例中的引用和期望的引用。如果它们两个引用是相同的(不使用 equals(),使用 ==进行比较),然后可以在AtomicReference实例上设置一个新的引用。
如果compareAndSet()在AtomicReference中设置一个新的引用,compareAndSet()方法返回true。否则,compareAndSet()返回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() be时刚刚设置的新引用,存储的引用当然不等于初始引用。因此,没有在AtomicReference上设置新的引用并且compareAndSet()方法返回false。

下一节:AtomicStampedReference
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值