前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。?
AtomicInteger 原子整數
可以原子更新的int值。
用于原子遞增計數器等應用程序中,不能用作java.lang.Integer的替換。
擴展了Number。# 1.繼承關系:public class AtomicInteger extends Number implements java.io.Serializable # 2. 屬性、代碼塊:// Unsafe的實例private static final Unsafe unsafe = Unsafe.getUnsafe();// value字段的偏移量private static final long valueOffset;// 得 value 的偏移量static {try {valueOffset = unsafe.objectFieldOffset(AtomicInteger.class.getDeclaredField("value"));} catch (Exception ex) { throw new Error(ex); }}// 用于存儲 int 的值。private volatile int value;# 3.方法:// 構造public AtomicInteger(int initialValue) {value = initialValue;}public AtomicInteger() {}public final int get() {return value;}public final void set(int newValue) {value = newValue;}// set的延遲實現,不保證值的改變立即被其他線程看到。public final void lazySet(int newValue) {unsafe.putOrderedInt(this, valueOffset, newValue);}// 原子地設置為給定值并返回舊值。public final int getAndSet(int newValue) {return unsafe.getAndSetInt(this, valueOffset, newValue);}// compareAndSwapInt方法參數:this操作的對象;// valueOffset對象中字段的偏移量;// expect原來的值,即期望的值;update要修改的值;public final boolean compareAndSet(int expect, int update) {return unsafe.compareAndSwapInt(this, valueOffset, expect, update);}// 原子地將值設置為給定的更新值public final boolean weakCompareAndSet(int expect, int update) {return unsafe.compareAndSwapInt(this, valueOffset, expect, update);}// 原子性地自增 1 。// 參數:this操作的對象;valueOffset對象中字段的偏移量; 1 要增加的值。// 返回的是未自增前的值。public final int getAndIncrement() {return unsafe.getAndAddInt(this, valueOffset, 1);}// 原子性地自減 1 。 返回的是未自減前的值。public final int getAndDecrement() {return unsafe.getAndAddInt(this, valueOffset, -1);}// 原子地將給定值添加到當前值。public final int getAndAdd(int delta) {return unsafe.getAndAddInt(this, valueOffset, delta);}// 原子性地自增 1 。 返回的是自增后的值。public final int incrementAndGet() {return unsafe.getAndAddInt(this, valueOffset, 1) + 1;}// // 原子性地自減 1 。 返回的是自減后的值。public final int decrementAndGet() {return unsafe.getAndAddInt(this, valueOffset, -1) - 1;}// 原子地將給定值添加到當前值。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;}// 原子性地使用將給定函數應用于當前值和給定值的結果更新當前值,并返回上一個值。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;}// 原子性地使用將給定函數應用于當前值和給定值的結果更新當前值,并返回更新后的值。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());}// 得int、long、float、double值。public int intValue() {return get();}public long longValue() {return (long)get();}public float floatValue() {return (float)get();}public double doubleValue() {return (double)get();}
?
其它 :
int a =0;
return ++a ;? 輸出 1。
return a++ ;? 輸出 0。
這是非原子性的操作。
?
?