/** * Atomically increments by one the current value. * * @return the previous value */ publicfinalintgetAndIncrement(){ return unsafe.getAndAddInt(this, valueOffset, 1); }
它的getAndIncrement调用了unsafe的getAndAddInt();
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/** *var1 是AtomicInteger本身 *var2 是该对象的内存偏移地址 *var4是要加的值 *var5是找主内存的真实值 */ publicfinalintgetAndAddInt(Object var1, long var2, int var4){ int var5; do { var5 = this.getIntVolatile(var1, var2); } while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4));
return var5; }
1
publicfinalnativebooleancompareAndSwapInt(Object var1, long var2, int var4, int var5);