public class AtomicReference
extends Object implements Serializable
| java.lang.Object | |
| ↳ | java.util.concurrent.atomic.AtomicReference<V> |
可以自动更新的对象引用。 有关原子变量属性的描述,请参阅java.util.concurrent.atomic包规范。
Public constructors |
|
|---|---|
AtomicReference(V initialValue) 用给定的初始值创建一个新的AtomicReference。 |
|
AtomicReference() 用空初始值创建一个新的AtomicReference。 |
|
Public methods |
|
|---|---|
final V |
accumulateAndGet(V x, BinaryOperator<V> accumulatorFunction) 将给定函数应用于当前值和给定值的结果以原子方式更新当前值,并返回更新后的值。 |
final boolean |
compareAndSet(V expect, V update) 如果当前值为 |
final V |
get() 获取当前值。 |
final V |
getAndAccumulate(V x, BinaryOperator<V> accumulatorFunction) 将给定函数应用于当前值和给定值的结果以原始方式更新当前值,并返回以前的值。 |
final V |
getAndSet(V newValue) 原子级设置为给定值并返回旧值。 |
final V |
getAndUpdate(UnaryOperator<V> updateFunction) 用应用给定函数的结果原子地更新当前值,返回以前的值。 |
final void |
lazySet(V newValue) 最终设置为给定值。 |
final void |
set(V newValue) 设置为给定值。 |
String |
toString() 返回当前值的字符串表示形式。 |
final V |
updateAndGet(UnaryOperator<V> updateFunction) 使用给定函数的结果原子地更新当前值,并返回更新的值。 |
final boolean |
weakCompareAndSet(V expect, V update) 如果当前值 |
Inherited methods |
|
|---|---|
java.lang.Object
|
|
AtomicReference (V initialValue)
用给定的初始值创建一个新的AtomicReference。
| Parameters | |
|---|---|
initialValue |
V: the initial value |
V accumulateAndGet (V x,
BinaryOperator<V> accumulatorFunction)
将给定函数应用于当前值和给定值的结果以原子方式更新当前值,并返回更新后的值。 该函数应该是无副作用的,因为当尝试更新由于线程之间的争用而失败时,它可能会被重新应用。 该函数以当前值作为第一个参数,给定更新作为第二个参数应用。
| Parameters | |
|---|---|
x |
V: the update value |
accumulatorFunction |
BinaryOperator: a side-effect-free function of two arguments |
| Returns | |
|---|---|
V |
the updated value |
boolean compareAndSet (V expect,
V update)
如果当前值为 ==的预期值, ==原子值将该值设置为给定的更新值。
| Parameters | |
|---|---|
expect |
V: the expected value |
update |
V: the new value |
| Returns | |
|---|---|
boolean |
true if successful. False return indicates that the actual value was not equal to the expected value. |
V getAndAccumulate (V x,
BinaryOperator<V> accumulatorFunction)
将给定函数应用于当前值和给定值的结果以原始方式更新当前值,并返回以前的值。 该函数应该是无副作用的,因为当尝试更新由于线程之间的争用而失败时,它可能会被重新应用。 该函数以当前值作为第一个参数,给定更新作为第二个参数应用。
| Parameters | |
|---|---|
x |
V: the update value |
accumulatorFunction |
BinaryOperator: a side-effect-free function of two arguments |
| Returns | |
|---|---|
V |
the previous value |
V getAndSet (V newValue)
原子级设置为给定值并返回旧值。
| Parameters | |
|---|---|
newValue |
V: the new value |
| Returns | |
|---|---|
V |
the previous value |
V getAndUpdate (UnaryOperator<V> updateFunction)
用应用给定函数的结果原子地更新当前值,返回以前的值。 该函数应该是无副作用的,因为当尝试更新由于线程之间的争用而失败时,它可能会被重新应用。
| Parameters | |
|---|---|
updateFunction |
UnaryOperator: a side-effect-free function |
| Returns | |
|---|---|
V |
the previous value |
void lazySet (V newValue)
最终设置为给定值。
| Parameters | |
|---|---|
newValue |
V: the new value |
String toString ()
返回当前值的字符串表示形式。
| Returns | |
|---|---|
String |
the String representation of the current value |
V updateAndGet (UnaryOperator<V> updateFunction)
使用给定函数的结果原子地更新当前值,并返回更新的值。 该函数应该是无副作用的,因为当尝试更新由于线程之间的争用而失败时,它可能会被重新应用。
| Parameters | |
|---|---|
updateFunction |
UnaryOperator: a side-effect-free function |
| Returns | |
|---|---|
V |
the updated value |
boolean weakCompareAndSet (V expect,
V update)
如果当前值 ==为期望值,则将该值 ==原子值设置为给定的更新值。
May fail spuriously and does not provide ordering guarantees ,所以只有很少的 compareAndSet替代选择。
| Parameters | |
|---|---|
expect |
V: the expected value |
update |
V: the new value |
| Returns | |
|---|---|
boolean |
true if successful |