|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
java.lang.Objectjava.util.concurrent.atomic.AtomicReferenceFieldUpdater<T,V>
T - 保持可更新字段的对象的类型V - 字段的类型public abstract class AtomicReferenceFieldUpdater<T,V>
基于反射的实用工具,可以对指定类的指定 volatile 字段进行原子更新。该类用于原子数据结构,该结构中同一节点的几个引用字段都独立受原子更新控制。例如,树节点可能声明为
class Node {
private volatile Node left, right;
private static final AtomicReferenceFieldUpdater leftUpdater =
AtomicReferenceFieldUpdater.newUpdater(Node.class, Node.class, "left");
private static AtomicReferenceFieldUpdater 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 的其他调用,它仅可以保证原子性和可变语义。
| 构造方法摘要 | |
|---|---|
protected |
AtomicReferenceFieldUpdater()
受保护的无操作构造方法,供子类使用。 |
| 方法摘要 | ||
|---|---|---|
abstract boolean |
compareAndSet(T obj,
V expect,
V update)
如果当前值 == 预期值,则以原子方式将此更新器管理的给定对象的字段设置为给定的更新值。 |
|
abstract V |
get(T obj)
获取由此更新器管理的在给定对象的字段中保持的当前值。 |
|
V |
getAndSet(T obj,
V newValue)
将此更新器管理的给定对象的字段自动设置为给定值,并返回旧值。 |
|
abstract void |
lazySet(T obj,
V newValue)
最终将此更新器管理的给定对象的字段设置为给定更新值。 |
|
static
|
newUpdater(Class<U> tclass,
Class<W> vclass,
String fieldName)
使用给定的字段为对象创建和返回一个更新器。 |
|
abstract void |
set(T obj,
V newValue)
将此更新器管理的给定对象的字段设置为给定更新值。 |
|
abstract boolean |
weakCompareAndSet(T obj,
V expect,
V update)
如果当前值 == 预期值,则以原子方式将此更新器管理的给定对象的字段设置为给定的更新值。 |
|
| 从类 java.lang.Object 继承的方法 |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| 构造方法详细信息 |
|---|
protected AtomicReferenceFieldUpdater()
| 方法详细信息 |
|---|
public static <U,W> AtomicReferenceFieldUpdater<U,W> newUpdater(Class<U> tclass,
Class<W> vclass,
String fieldName)
tclass - 保持字段的对象类。vclass - 该字段的类fieldName - 要更新的字段名称。
IllegalArgumentException - 如果该字段不是可变引用类型。
RuntimeException - 如果该类不保持字段,或者是错误的类型,将抛出 RuntimeException 和一个嵌套的基于反射的异常。
public abstract boolean compareAndSet(T obj,
V expect,
V update)
== 预期值,则以原子方式将此更新器管理的给定对象的字段设置为给定的更新值。对 compareAndSet 和 set 的其他调用,此方法可以确保原子性,但对于字段中的其他更改则不一定确保原子性。
obj - 有条件地设置其字段的对象expect - 预期值update - 新值
public abstract boolean weakCompareAndSet(T obj,
V expect,
V update)
== 预期值,则以原子方式将此更新器管理的给定对象的字段设置为给定的更新值。对 compareAndSet 和 set 的其他调用,此方法可以确保原子性,但对于字段中的其他更改则不一定确保原子性。
可能意外失败并且不提供排序保证,所以只有在很少的情况下才对 compareAndSet 进行适当地选择。
obj - 有条件地设置其字段的对象expect - 预期值update - 新值
public abstract void set(T obj,
V newValue)
compareAndSet 的后续调用,此操作可以确保充当可变存储。
obj - 要设置其字段的对象newValue - 新值
public abstract void lazySet(T obj,
V newValue)
obj - 要设置其字段的对象newValue - 新值public abstract V get(T obj)
obj - 要获取其字段的对象
public V getAndSet(T obj,
V newValue)
obj - 要获取并设置其字段的对象newValue - 新值
|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策。