T
-客体可更新的字段的类型
V
-字段的类型
public abstract class AtomicReferenceFieldUpdater<T,V> extends Object
volatile
参考字段指定的类。此类被设计用于原子数据结构中,在该结构中,同一节点的几个参考字段是独立于原子更新的。例如,一个树节点可以声明为
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
}
值得注意的是,在这类的compareAndSet
方法保证弱于其他原子类。因为这类不能确保所有的字段都适合原子访问的目的,它可以保证原子性只有在同一更新相对于compareAndSet
和set
其他调用。
Modifier | Constructor and Description |
---|---|
protected |
AtomicReferenceFieldUpdater()
受保护的无构造函数使用的子类。
|
Modifier and Type | Method and Description |
---|---|
V |
accumulateAndGet(T obj, V x, BinaryOperator<V> accumulatorFunction)
自动更新给定对象的更新管理与应用给出的函数的电流与给定值结果领域,返回更新后的值。
|
abstract boolean |
compareAndSet(T obj, V expect, V update)
自动设置给定对象的这个更新设法给出更新的价值领域如果电流值
== 期望值。
|
abstract V |
get(T obj)
获取当前值在给定对象的更新管理领域举行。
|
V |
getAndAccumulate(T obj, V x, BinaryOperator<V> accumulatorFunction)
自动更新给定对象的更新管理与应用给出的函数的电流与给定值结果领域,返回前一个值。
|
V |
getAndSet(T obj, V newValue)
自动设置现场给定对象的更新管理给定的值并返回旧值。
|
V |
getAndUpdate(T obj, UnaryOperator<V> updateFunction)
自动更新领域的给定对象的更新管理与应用结果给定的函数,返回前一个值。
|
abstract void |
lazySet(T obj, V newValue)
最终设置给定对象的更新设法给出更新的价值领域。
|
static <U,W> AtomicReferenceFieldUpdater<U,W> |
newUpdater(类<U> tclass, 类<W> vclass, String fieldName)
创建并返回与给定的领域对象的更新。
|
abstract void |
set(T obj, V newValue)
设置给定对象的更新设法给出更新的价值领域。
|
V |
updateAndGet(T obj, UnaryOperator<V> updateFunction)
自动更新领域的给定对象的更新管理与应用结果给定的函数,返回更新后的值。
|
abstract boolean |
weakCompareAndSet(T obj, V expect, V update)
自动设置给定对象的这个更新设法给出更新的价值领域如果电流值
== 期望值。
|
protected AtomicReferenceFieldUpdater()
public static <U,W> AtomicReferenceFieldUpdater<U,W> newUpdater(类<U> tclass, 类<W> vclass, String fieldName)
U
-实例类型TClass
W
- V类类型的实例
tclass
-控股的领域对象的类
vclass
-场的类
fieldName
-被更新的字段的名称
ClassCastException
-如果字段的类型错误
IllegalArgumentException
如果字段不挥发
RuntimeException
-嵌套的基于反射的异常,如果类没有场或是错误的类型,或该领域无法调用java语言根据访问控制
public abstract boolean compareAndSet(T obj, V expect, V update)
==
期望值。这种方法可以保证原子相对于其他呼叫
compareAndSet
和
set
,但不一定就在该领域的其他变化。
obj
-一个对象的字段条件设置
expect
-期望值
update
-新价值
true
如果成功
public abstract boolean weakCompareAndSet(T obj, V expect, V update)
==
期望值。这种方法可以保证原子相对于其他呼叫
compareAndSet
和
set
,但不一定就在该领域的其他变化。
May fail spuriously and does not provide ordering guarantees,所以只有很少compareAndSet
适当的替代。
obj
-一个对象的字段条件设置
expect
-期望值
update
-新价值
true
如果成功
public abstract void set(T obj, V newValue)
compareAndSet
挥发性存储。
obj
-一个对象的域设置
newValue
-新价值
public abstract void lazySet(T obj, V newValue)
obj
-一个对象的字段
newValue
-新价值
public V getAndSet(T obj, V newValue)
obj
-一个对象的字段来获取和设置
newValue
-新价值
public final V getAndUpdate(T obj, UnaryOperator<V> updateFunction)
obj
-一个对象的字段来获取和设置
updateFunction
-无副作用的功能
public final V updateAndGet(T obj, UnaryOperator<V> updateFunction)
obj
-一个对象的字段来获取和设置
updateFunction
-无副作用的功能
public final V getAndAccumulate(T obj, V x, BinaryOperator<V> accumulatorFunction)
obj
-一个对象的字段来获取和设置
x
-更新价值
accumulatorFunction
-两参数无副作用的功能
public final V accumulateAndGet(T obj, V x, BinaryOperator<V> accumulatorFunction)
obj
-一个对象的字段来获取和设置
x
-更新价值
accumulatorFunction
-两参数无副作用的功能
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.