标签:地址 mic 修改 exp 流水线 精确 也会 分割 obj
AtomicInteger ai = new AtomicInteger(5); System.out.println(ai.compareAndSet(5, 2019) + " 当前值:" + ai.get()); // true 当前值:2019 System.out.println(ai.compareAndSet(5, 2020) + " 当前值:" + ai.get()); // false 当前值:2019 System.out.println(ai.compareAndSet(2019, 2020) + " 当前值:" + ai.get()); // true 当前值:2020 System.out.println(ai.getAndIncrement()); // 2020 System.out.println(ai.getAndIncrement()); // 2021
public class AtomicInteger extends Number implements java.io.Serializable { private static final long serialVersionUID = 6214790243416807050L; // setup to use Unsafe.compareAndSwapInt for updates private static final Unsafe unsafe = Unsafe.getUnsafe(); private static final long valueOffset; static { try { valueOffset = unsafe.objectFieldOffset (AtomicInteger.class.getDeclaredField("value")); } catch (Exception ex) { throw new Error(ex); } } // 保证可见性 private volatile int value; // ...省略很多代码 public final boolean compareAndSet(int expect, int update) { return unsafe.compareAndSwapInt(this, valueOffset, expect, update); } // 每调用一次自增1 public final int getAndIncrement() { // this为当前对象,valueOffset 为内存偏移量,即内存地址 return unsafe.getAndAddInt(this, valueOffset, 1); } }
UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x)) UnsafeWrapper("Unsafe_CompareAndSwapInt"); oop p = JNIHandles::resolve(obj); jint* addr = (jint *) index_oop_from_field_offset_long(p, offset); return (jint)(Atomic::cmpxchg(x, addr, e)) == e; UNSAFE_END
/** * var1为CAS操作的对象 * offset为var1某个属性的地址偏移值 * expected为期望值 * var2为要设置的值,利用JNI来完成CPU指令的操作 */ public final native boolean compareAndSwapObject(Object var1, long offset, Object expected, Object var2); public final native boolean compareAndSwapInt(Object var1, long offset, int expected, int var2); public final native boolean compareAndSwapLong(Object var1, long offset, long expected, long var2);
/** 如果CAS成功,return oldValue, oldValue = oldValue + addValue * 如果CAS失败,自旋,一直运行,直到成功为止 */ public final Xxx getAndAddXxx(Object var1, long offset, long addValue) { int oldValue; do { oldValue = this.getIntVolatile(var1, offset); } while(!this.compareAndSwapInt(var1, offset, oldValue, oldValue + addValue)); return oldValue; } /** 如果CAS成功,return oldValue, oldValue = newValue * 如果CAS失败,自旋,一直运行,直到成功为止 */ public final Xxx getAndSetXxx(Object var1, long offset, Object newValue) { int oldValue; do { oldValue = this.getXxxVolatile(var1, offset); } while(!this.compareAndSwapXxx(var1, offset, oldValue, newValue)); return oldValue; }
public class AtomicInteger extends Number implements java.io.Serializable { private static final long serialVersionUID = 6214790243416807050L; // setup to use Unsafe.compareAndSwapInt for updates // Unsafe类,提供一系列增强Java的功能,如内存管理、操作类/对象/变量、多线程同步等。不建议开发者调用 private static final Unsafe unsafe = Unsafe.getUnsafe(); // 获取对象某个属性的地址偏移值 private static final long valueOffset; static { try { // value相对“起始地址”的偏移量 valueOffset = unsafe.objectFieldOffset (AtomicInteger.class.getDeclaredField("value")); } catch (Exception ex) { throw new Error(ex); } } // value值, volatile修饰,保证不同线程间的可见性 private volatile int value; public AtomicInteger(int initialValue) { value = initialValue; } public AtomicInteger() {} public final int get() { return value; } public final void set(int newValue) { value = newValue; } /** * Eventually sets to the given value. * * @param newValue the new value * @since 1.6 */ public final void lazySet(int newValue) { //有序或者有延迟的putIntVolatile方法 unsafe.putOrderedInt(this, valueOffset, newValue); } /** * Atomically sets to the given value and returns the old value. * @param newValue the new value * @return the previous value */ public final int getAndSet(int newValue) { return unsafe.getAndSetInt(this, valueOffset, newValue); } /** * Atomically sets the value to the given updated value * if the current value {@code ==} the expected value. * @param expect the expected value * @param update the new value * @return {@code true} if successful. False return indicates that * the actual value was not equal to the expected value. */ public final boolean compareAndSet(int expect, int update) { // JNI调用,实现CAS return unsafe.compareAndSwapInt(this, valueOffset, expect, update); } /** * i++ 操作 * Atomically increments by one the current value. * @return the previous value */ public final int getAndIncrement() { return unsafe.getAndAddInt(this, valueOffset, 1); } /** * i-- 操作 * Atomically decrements by one the current value. * @return the previous value */ public final int getAndDecrement() { return unsafe.getAndAddInt(this, valueOffset, -1); } /** * return i, i = i + n 操作 * Atomically adds the given value to the current value. * @param delta the value to add * @return the previous value */ public final int getAndAdd(int delta) { return unsafe.getAndAddInt(this, valueOffset, delta); } /** * ++i 操作 * Atomically increments by one the current value. * @return the updated value */ public final int incrementAndGet() { return unsafe.getAndAddInt(this, valueOffset, 1) + 1; } /** * --i 操作 * Atomically decrements by one the current value. * @return the updated value */ public final int decrementAndGet() { return unsafe.getAndAddInt(this, valueOffset, -1) - 1; } /** * i = i + n ,return i操作 * Atomically adds the given value to the current value. * @param delta the value to add * @return the updated value */ public final int addAndGet(int delta) { return unsafe.getAndAddInt(this, valueOffset, delta) + delta; } // 其余函数,略...
标签:地址 mic 修改 exp 流水线 精确 也会 分割 obj
原文地址:https://www.cnblogs.com/caoxb/p/13139508.html