开篇
AtomicLong位于java.util.concurrent.atomic包下,是java提供给的可以保证数据的原子性操作的一个类。和AtomicInteger的实现几乎是一致的。底层同样借助于unsafe实现原子操作。
据说sun.misc.Unsafe类库里面的 CAS算法,是通过用CPU指令来实现无锁自增,没仔细研究过所以暂时不展开。
AtomicLong类和构造器
AtomicLong类构造器有两个:
- 无参构造函数采用默认值初始化为0
- 有参数构造函数直接用initialValue来value的
AtomicLong类变量需要注意的两个点:
- AtomicLong的关键逻辑在于static代码快中通过unsafe接口初始化value的内存地址,后续直接通过内存地址进行操作。
- AtomicLong的value是用volatile进行修饰保证变量的可见性
public class AtomicLong extends Number implements java.io.Serializable {
private static final long serialVersionUID = 1927816293512124184L;
private static final Unsafe unsafe = Unsafe.getUnsafe();
// 保存AtomicLong中value的内存地址便于快速操作
private static final long valueOffset;
static final boolean VM_SUPPORTS_LONG_CAS = VMSupportsCS8();
private static native boolean VMSupportsCS8();
// 获取value的内存地址的逻辑操作
static {
try {
valueOffset = unsafe.objectFieldOffset
(AtomicLong.class.getDeclaredField("value"));
} catch (Exception ex) { throw new Error(ex); }
}
private volatile long value;
// 以initialValue对value进行初始化
public AtomicLong(long initialValue) {
value = initialValue;
}
// 以默认值0对value进行初始化
public AtomicLong() {
}
}
AtomicLong的get操作
AtomicLong类的get动作包括直接返回值和返回值后自增自减两类。在原子性的实现自增自减少的过程中通过unsafe. getAndAddLong()方法进行值的设置。
unsafe.getAndAddLong()方法的内部首先是获取旧的value并且通过compareAndSwapLong()方法保证在旧值v不变的基础上才能改为新值v+delta。
public final long getAndAddLong(Object o, long offset, long delta) {
long v;
do {
v = getLongVolatile(o, offset);
} while (!compareAndSwapLong(o, offset, v, v + delta));
return v;
}
------------------------------------------------------------
public final long get() {
return value;
}
public final long getAndSet(long newValue) {
return unsafe.getAndSetLong(this, valueOffset, newValue);
}
public final long getAndIncrement() {
return unsafe.getAndAddLong(this, valueOffset, 1L);
}
public final long getAndDecrement() {
return unsafe.getAndAddLong(this, valueOffset, -1L);
}
public final long getAndAdd(long delta) {
return unsafe.getAndAddLong(this, valueOffset, delta);
}
public final long getAndUpdate(LongUnaryOperator updateFunction) {
long prev, next;
do {
prev = get();
next = updateFunction.applyAsLong(prev);
} while (!compareAndSet(prev, next));
return prev;
}
public final long getAndAccumulate(long x, LongBinaryOperator accumulatorFunction) {
long prev, next;
do {
prev = get();
next = accumulatorFunction.applyAsLong(prev, x);
} while (!compareAndSet(prev, next));
return prev;
}
AtomicLong的set操作
AtomicLong类的set动作包括直接返回值和返回自增自减后的值两类。原子性实现自增自减是借助unsafe.getAndAddLong()方法实现的。
unsafe.getAndAddLong()方法的内部首先是获取旧的value并且通过compareAndSwapLong()方法保证在旧值v不变的基础上才能改为新值v+delta。
针对incrementAndGet()的实现细节,有一点比较有意思,unsafe.getAndAddLong(this, valueOffset, 1L) + 1L,因为unsafe.getAndAddLong返回的是旧值然后通过+delta实现最终的值。而此时对应value的值通过getAndAddLong()修改成了新值,也就说其实incrementAndGet()方法取的值并不是value的内存地址中对应的值。
public final long getAndAddLong(Object o, long offset, long delta) {
long v;
do {
v = getLongVolatile(o, offset);
} while (!compareAndSwapLong(o, offset, v, v + delta));
return v;
}
------------------------------------------------------------
public final void set(long newValue) {
value = newValue;
}
public final void lazySet(long newValue) {
unsafe.putOrderedLong(this, valueOffset, newValue);
}
public final long incrementAndGet() {
return unsafe.getAndAddLong(this, valueOffset, 1L) + 1L;
}
public final long decrementAndGet() {
return unsafe.getAndAddLong(this, valueOffset, -1L) - 1L;
}
public final long addAndGet(long delta) {
return unsafe.getAndAddLong(this, valueOffset, delta) + delta;
}
public final long updateAndGet(LongUnaryOperator updateFunction) {
long prev, next;
do {
prev = get();
next = updateFunction.applyAsLong(prev);
} while (!compareAndSet(prev, next));
return next;
}
public final long accumulateAndGet(long x, LongBinaryOperator accumulatorFunction) {
long prev, next;
do {
prev = get();
next = accumulatorFunction.applyAsLong(prev, x);
} while (!compareAndSet(prev, next));
return next;
}