简体   繁体   English

AtomicReference与AtomicReferenceFieldUpdater,AtomicReferenceFieldUpdater的目的是什么?

[英]AtomicReference vs AtomicReferenceFieldUpdater, what's a purpose of AtomicReferenceFieldUpdater?

I'd like to atomically upgrade my reference. 我想自动升级我的参考。 For example to use compareAndSet , getAndSet and other atomic operations. 例如,使用compareAndSetgetAndSet和其他原子操作。

I came from C++, so in C++ I've got volatile keyword and different atomic intrinsics, or the <atomic> API . 我来自C ++,所以在C ++中,我有volatile关键字和不同的原子内在函数,或者是<atomic> API In java, there's also a volatile keyword and different unsafe atomic operations . 在Java中,还有一个volatile关键字和不同的不安全原子操作

By the way, there's also a well-documented AtomicReference (as well as Long , Integer , Boolean ), so JDK creators provided us a way to safely execute atomic operations against references and primitives. 顺便说一下,还有一个文档齐全的AtomicReference (以及LongIntegerBoolean ),因此JDK的创建者为我们提供了一种对引用和基元安全地执行原子操作的方法。 There's nothing wrong about the API, it is rich and seems very familiar. API没什么错,它很丰富而且看起来很熟悉。

But, there's also an AtomicReferenceFieldUpdater whick provides a kinda weird way to execute atomic operations: you have to "find" the field via reflection by name and then you can use exactly the same operations. 但是,还有一个AtomicReferenceFieldUpdater鞭子提供了一种执行原子操作的怪异方法:您必须通过名称反射来“查找”字段,然后可以使用完全相同的操作。

So my questions are: 所以我的问题是:

  1. What's the purpose of AtomicReferenceFieldUpdater at all? AtomicReferenceFieldUpdater的目的是什么? In my opinion it is implicit and kinda weird: you need to declare a volatile variable filed AND and the field updater itself. 在我看来,它是隐式的并且有点奇怪:您需要声明一个可变变量字段AND和字段更新程序本身。 So where should I use a FieldUpdater ? 那么我应该在哪里使用FieldUpdater
  2. It provides an indirection: you're manipulating (not changing !) the FieldUpdater , not the variable, this confuses. 它提供了一种间接方式:您正在操纵 (而不是更改 !)这会造成混淆的FieldUpdater而不是变量。
  3. Performance: as far as I know, both AtomicReferenceFieldUpdater and AtomicReference are delegating to the Unsafe, so their performance is similar, but anyway: are there any performance penalties of FieldUpdater agaist Reference? 性能:据我所知, AtomicReferenceFieldUpdaterAtomicReference均委托给Unsafe,因此它们的性能相似,但无论如何: FieldUpdater Reference是否有性能损失?

It's used to save memory. 它用于节省内存。

Example from the doc: doc中的示例

class Node {
   private volatile Node left, right;

   private static final AtomicReferenceFieldUpdater<Node, Node> leftUpdater =
     AtomicReferenceFieldUpdater.newUpdater(Node.class, Node.class, "left");
   private static AtomicReferenceFieldUpdater<Node, Node> rightUpdater =
     AtomicReferenceFieldUpdater.newUpdater(Node.class, Node.class, "right");

   Node getLeft() { return left; }
   boolean compareAndSetLeft(Node expect, Node update) {
     return leftUpdater.compareAndSet(this, expect, update);
   }
   // ... and so on
 }

it declares left and right as Node directly. 它直接将leftright声明为Node And the AtomicReferenceFieldUpdater is static final . 并且AtomicReferenceFieldUpdaterstatic final

Without AtomicReferenceFieldUpdater , you might need declare them as AtomicReference<Node> . 如果没有AtomicReferenceFieldUpdater ,则可能需要将它们声明为AtomicReference<Node>

private  AtomicReference<Node> left, right;

which consumes more memory than Node . 它比Node消耗更多的内存 When there are many instances of Node , it consumes much more memory than first approach. Node实例很多时,它比第一种方法消耗更多的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM