AtomicInteger源码非常简单,仅仅使用了一个volatile类型的值和一个static类型的偏移量完成整个原子int操作。
其中,valueoffset用来记录value字段相对于整个类对象起始位置的偏移量,这个在class被加载到内存的时候就已经确定了,因此使用static类型的值,final保证了数据的可见性。
value使用volatile,通过volatile内存屏障保证value类型的数据实时可见,并且简化set和get操作。
public class AtomicInteger extends Number implements java.io.Serializable {
private static final long serialVersionUID = 6214790243416807050L;
private static final Unsafe unsafe = Unsafe.getUnsafe();
private static final long valueOffset;
//类被加载加来之后,对象的value字段位置相对于对象的位置就确定了,因此放在static块中。
static {
try {
valueOffset = unsafe.objectFieldOffset
(AtomicInteger.class.getDeclaredField("value"));
} catch (Exception ex) { throw new Error(ex); }
}
//当前值,volatile保证线程可见,compareAndSet通过valueOffset cas指令修改,强制set时直接set。
private volatile int value;
public AtomicInteger(int initialValue) {
value = initialValue;
}
public AtomicInteger() {
}
//value线程可见,此处返回的是此时可见的最新值。
public final int get() {
return value;
}
//直接强制set,根据volatile分析,此处前后会有内存屏障保证数据正确写入。
public final void set(int newValue) {
value = newValue;
}
//懒set,最终会成功set,但是相对于直接set,少了一层内存屏障(storeload),不能保证set之后的读一定正确。
public final void lazySet(int newValue) {
unsafe.putOrderedInt(this, valueOffset, newValue);
}
//返回旧值,设置新值
public final int getAndSet(int newValue) {
return unsafe.getAndSetInt(this, valueOffset, newValue);
}
//CAS指令,如果等于expect,就设置为update
public final boolean compareAndSet(int expect, int update) {
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
}
//目前实现同compareAndSet,可能是预保留的接口
public final boolean weakCompareAndSet(int expect, int update) {
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
}
//a++
public final int getAndIncrement() {
return unsafe.getAndAddInt(this, valueOffset, 1);
}
//a--
public final int getAndDecrement() {
return unsafe.getAndAddInt(this, valueOffset, -1);
}
//返回当前值,并将当前值+delta
public final int getAndAdd(int delta) {
return unsafe.getAndAddInt(this, valueOffset, delta);
}
//++a
public final int incrementAndGet() {
return unsafe.getAndAddInt(this, valueOffset, 1) + 1;
}
//--a
public final int decrementAndGet() {
return unsafe.getAndAddInt(this, valueOffset, -1) - 1;
}
//将当前值+delta后返回
public final int addAndGet(int delta) {
return unsafe.getAndAddInt(this, valueOffset, delta) + delta;
}
//将当前值更新为方法返回值,返回更新前的值,要考虑方法会多次执行
public final int getAndUpdate(IntUnaryOperator updateFunction) {
int prev, next;
do {
prev = get();
next = updateFunction.applyAsInt(prev);
} while (!compareAndSet(prev, next));
return prev;
}
//将当前值更新为方法返回值,返回更新后的值,要考虑方法会多次执行
public final int updateAndGet(IntUnaryOperator updateFunction) {
int prev, next;
do {
prev = get();
next = updateFunction.applyAsInt(prev);
} while (!compareAndSet(prev, next));
return next;
}
//实现同getAndUpdate
public final int getAndAccumulate(int x,
IntBinaryOperator accumulatorFunction) {
int prev, next;
do {
prev = get();
next = accumulatorFunction.applyAsInt(prev, x);
} while (!compareAndSet(prev, next));
return prev;
}
//实现同updateAndGet
public final int accumulateAndGet(int x,
IntBinaryOperator accumulatorFunction) {
int prev, next;
do {
prev = get();
next = accumulatorFunction.applyAsInt(prev, x);
} while (!compareAndSet(prev, next));
return next;
}
public String toString() {
return Integer.toString(get());
}
public int intValue() {
return get();
}
public long longValue() {
return (long)get();
}
public float floatValue() {
return (float)get();
}
public double doubleValue() {
return (double)get();
}
}