public abstract class AtomicIntegerFieldUpdater
extends Object
| java.lang.Object | |
| java.util.concurrent.atomic.AtomicIntegerFieldUpdater<T> | |
基于反射的实用程序,可以将原子更新到指定类的指定volatile int字段。 此类专用于原子数据结构,其中同一节点的多个字段独立进行原子更新。
请注意,该类中的compareAndSet方法的保证比其他原子类中的方法更弱。 由于该类无法确保该字段的所有用途都适用于原子访问,因此只能针对同一更新程序上的其他调用compareAndSet和set保证原子性。
Protected constructors |
|
|---|---|
AtomicIntegerFieldUpdater() 受保护的无所事事的构造函数供子类使用。 |
|
公共方法(Public methods) |
|
|---|---|
final int |
accumulateAndGet(T obj, int x, IntBinaryOperator accumulatorFunction) 使用给定函数应用于当前值和给定值的结果,原子更新由此更新程序管理的给定对象的字段,并返回更新后的值。 |
int |
addAndGet(T obj, int delta) 以原子方式将给定值添加到由此更新程序管理的给定对象的字段的当前值。 |
abstract boolean |
compareAndSet(T obj, int expect, int update) 如果当前值 |
int |
decrementAndGet(T obj) 按原子法递减由此更新程序管理的给定对象的字段的当前值。 |
abstract int |
get(T obj) 获取由此更新器管理的给定对象的字段中保存的当前值。 |
final int |
getAndAccumulate(T obj, int x, IntBinaryOperator accumulatorFunction) 使用给定函数应用到当前值和给定值的结果,原子地更新由此更新程序管理的给定对象的字段,并返回以前的值。 |
int |
getAndAdd(T obj, int delta) 以原子方式将给定值添加到由此更新程序管理的给定对象的字段的当前值。 |
int |
getAndDecrement(T obj) 按原子法递减由此更新程序管理的给定对象的字段的当前值。 |
int |
getAndIncrement(T obj) 以原子为单位递增由此更新程序管理的给定对象的字段的当前值。 |
int |
getAndSet(T obj, int newValue) 以原子方式将由此更新程序管理的给定对象的字段设置为给定值并返回旧值。 |
final int |
getAndUpdate(T obj, IntUnaryOperator updateFunction) 使用给定函数的结果原子更新由此更新程序管理的给定对象的字段,并返回以前的值。 |
int |
incrementAndGet(T obj) 以原子为单位递增由此更新程序管理的给定对象的字段的当前值。 |
abstract void |
lazySet(T obj, int newValue) 最终将由此更新程序管理的给定对象的字段设置为给定的更新值。 |
static <U> AtomicIntegerFieldUpdater<U> |
newUpdater(Class<U> tclass, String fieldName) 创建并返回具有给定字段的对象的更新程序。 |
abstract void |
set(T obj, int newValue) 将由此更新程序管理的给定对象的字段设置为给定的更新值。 |
final int |
updateAndGet(T obj, IntUnaryOperator updateFunction) 使用给定函数的结果原子更新由此更新程序管理的给定对象的字段,并返回更新后的值。 |
abstract boolean |
weakCompareAndSet(T obj, int expect, int update) 如果当前值 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
int accumulateAndGet (T obj,
int x,
IntBinaryOperator accumulatorFunction)
使用给定函数应用于当前值和给定值的结果,原子更新由此更新程序管理的给定对象的字段,并返回更新后的值。 该函数应该是无副作用的,因为当尝试更新由于线程之间的争用而失败时,它可能会被重新应用。 该函数以当前值作为第一个参数,给定更新作为第二个参数应用。
| 参数(Parameters) | |
|---|---|
obj |
T: An object whose field to get and set |
x |
int: the update value |
accumulatorFunction |
IntBinaryOperator: a side-effect-free function of two arguments |
| 返回(Returns) | |
|---|---|
int |
the updated value |
int addAndGet (T obj,
int delta)
以原子方式将给定值添加到由此更新程序管理的给定对象的字段的当前值。
| 参数(Parameters) | |
|---|---|
obj |
T: An object whose field to get and set |
delta |
int: the value to add |
| 返回(Returns) | |
|---|---|
int |
the updated value |
boolean compareAndSet (T obj,
int expect,
int update)
如果当前值==为期望值,则将由此更新程序管理的给定对象的字段按原子级设置为给定的更新值。 对于其他compareAndSet和set调用,这种方法保证是原子的,但不一定与该领域的其他变化有关。
| 参数(Parameters) | |
|---|---|
obj |
T: An object whose field to conditionally set |
expect |
int: the expected value |
update |
int: the new value |
| 返回(Returns) | |
|---|---|
boolean |
true if successful |
| 抛出异常(Throws) | |
|---|---|
ClassCastException |
if obj is not an instance of the class possessing the field established in the constructor |
int decrementAndGet (T obj)
按原子法递减由此更新程序管理的给定对象的字段的当前值。
| 参数(Parameters) | |
|---|---|
obj |
T: An object whose field to get and set |
| 返回(Returns) | |
|---|---|
int |
the updated value |
int get (T obj)
获取由此更新器管理的给定对象的字段中保存的当前值。
| 参数(Parameters) | |
|---|---|
obj |
T: An object whose field to get |
| 返回(Returns) | |
|---|---|
int |
the current value |
int getAndAccumulate (T obj,
int x,
IntBinaryOperator accumulatorFunction)
使用给定函数应用到当前值和给定值的结果,原子地更新由此更新程序管理的给定对象的字段,并返回以前的值。 该函数应该是无副作用的,因为当尝试更新由于线程之间的争用而失败时,它可能会被重新应用。 该函数以当前值作为第一个参数,给定更新作为第二个参数应用。
| 参数(Parameters) | |
|---|---|
obj |
T: An object whose field to get and set |
x |
int: the update value |
accumulatorFunction |
IntBinaryOperator: a side-effect-free function of two arguments |
| 返回(Returns) | |
|---|---|
int |
the previous value |
int getAndAdd (T obj,
int delta)
以原子方式将给定值添加到由此更新程序管理的给定对象的字段的当前值。
| 参数(Parameters) | |
|---|---|
obj |
T: An object whose field to get and set |
delta |
int: the value to add |
| 返回(Returns) | |
|---|---|
int |
the previous value |
int getAndDecrement (T obj)
按原子法递减由此更新程序管理的给定对象的字段的当前值。
| 参数(Parameters) | |
|---|---|
obj |
T: An object whose field to get and set |
| 返回(Returns) | |
|---|---|
int |
the previous value |
int getAndIncrement (T obj)
以原子为单位递增由此更新程序管理的给定对象的字段的当前值。
| 参数(Parameters) | |
|---|---|
obj |
T: An object whose field to get and set |
| 返回(Returns) | |
|---|---|
int |
the previous value |
int getAndSet (T obj,
int newValue)
以原子方式将由此更新程序管理的给定对象的字段设置为给定值并返回旧值。
| 参数(Parameters) | |
|---|---|
obj |
T: An object whose field to get and set |
newValue |
int: the new value |
| 返回(Returns) | |
|---|---|
int |
the previous value |
int getAndUpdate (T obj,
IntUnaryOperator updateFunction)
使用给定函数的结果原子更新由此更新程序管理的给定对象的字段,并返回以前的值。 该函数应该是无副作用的,因为当尝试更新由于线程之间的争用而失败时,它可能会被重新应用。
| 参数(Parameters) | |
|---|---|
obj |
T: An object whose field to get and set |
updateFunction |
IntUnaryOperator: a side-effect-free function |
| 返回(Returns) | |
|---|---|
int |
the previous value |
int incrementAndGet (T obj)
以原子为单位递增由此更新程序管理的给定对象的字段的当前值。
| 参数(Parameters) | |
|---|---|
obj |
T: An object whose field to get and set |
| 返回(Returns) | |
|---|---|
int |
the updated value |
void lazySet (T obj,
int newValue)
最终将由此更新程序管理的给定对象的字段设置为给定的更新值。
| 参数(Parameters) | |
|---|---|
obj |
T: An object whose field to set |
newValue |
int: the new value |
AtomicIntegerFieldUpdater<U> newUpdater (Class<U> tclass, String fieldName)
创建并返回具有给定字段的对象的更新程序。 需要Class参数来检查反射类型和泛型类型是否匹配。
| 参数(Parameters) | |
|---|---|
tclass |
Class: the class of the objects holding the field |
fieldName |
String: the name of the field to be updated |
| 返回(Returns) | |
|---|---|
AtomicIntegerFieldUpdater<U> |
the updater |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if the field is not a volatile integer type |
RuntimeException |
with a nested reflection-based exception if the class does not hold field or is the wrong type, or the field is inaccessible to the caller according to Java language access control |
void set (T obj,
int newValue)
将由此更新程序管理的给定对象的字段设置为给定的更新值。 此操作保证充当对后续调用compareAndSet的不稳定存储。
| 参数(Parameters) | |
|---|---|
obj |
T: An object whose field to set |
newValue |
int: the new value |
int updateAndGet (T obj,
IntUnaryOperator updateFunction)
使用给定函数的结果原子更新由此更新程序管理的给定对象的字段,并返回更新后的值。 该函数应该是无副作用的,因为当尝试更新由于线程之间的争用而失败时,它可能会被重新应用。
| 参数(Parameters) | |
|---|---|
obj |
T: An object whose field to get and set |
updateFunction |
IntUnaryOperator: a side-effect-free function |
| 返回(Returns) | |
|---|---|
int |
the updated value |
boolean weakCompareAndSet (T obj,
int expect,
int update)
如果当前值为== ,则以原子方式将此更新程序管理的给定对象的字段设置为给定的更新值。 这种方法对于其他compareAndSet和set调用而言是保证原子的,但不一定与该领域的其他变化有关。
May fail spuriously and does not provide ordering guarantees ,所以只是 compareAndSet一个 compareAndSet 。
| 参数(Parameters) | |
|---|---|
obj |
T: An object whose field to conditionally set |
expect |
int: the expected value |
update |
int: the new value |
| 返回(Returns) | |
|---|---|
boolean |
true if successful |
| 抛出异常(Throws) | |
|---|---|
ClassCastException |
if obj is not an instance of the class possessing the field established in the constructor |