目录

AtomicReference原子性引用浅析(持续更新)

一、前言

  • AtomicReference原子性引用,一般情况下用不到,更多的是在使用多线程操作修改数据时会用到
  • Java文档描述为:An object reference that may be updated atomically.(一个可以被原子性更新的对象引用)

二、方法说明

返回值 方法名 英文描述 中文描述
final V accumulateAndGet(V x, BinaryOperator<V> accumulatorFunction) Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value. 通过当前值和给定的值来原子性的更新当前值,返回更新后的值
final boolean compareAndSet(V expect, V update) Atomically sets the value to the given updated value if the current value == the expected value. 如果当前值等于给定的值,则原子性地设置新的值
final V get() Gets the current value 获得当前值
final V getAndAccumulate(V x,BinaryOperator<V> accumulatorFunction) Atomically updates the current value with the results of applying the given function to the current and given values, returning the previous value. 通过当前值和给定的值来原子性的更新当前值,返回更新前的值
final V getAndSet(V newValue) Atomically sets to the given value and returns the old value. 原子性地设置给予的值并返回更新前的值
final V getAndUpdate(UnaryOperator<V> updateDunction) Atomically updates the current value with the results of applying the given function, returning the previous value. 根据给定的值原子性地更新当前值,返回更新前的值
final void lazySet(V newValue) Eventually sets to the given value. 最终设置为给予的值
final void set(V newValue) Sets to the give value 设置为给予的值
final V updateAndGet(UnaryOperator<V> updateFunction) Atomically updates the current value with the results of applying the given function, returning the updated value. 根据给定的值原子性地更新当前值,返回更新后的值
final boolean weakCompareAndSet(V expect,V update) Atomically sets the value to the given updated value if the current value == the expected value. 如果当前值等于给定的值,则原子性的设置更新值