并发编程--原子类AtomicLong、AtomicBoolean和AtomicReference



在上一篇博客 并发编程--原子类AotmicInteger中我们已经简单介绍了一下AtomicInteger相关的知识,简单来说AtomicLong的实现原理与AtomicInteger是相同的,用volatile来修饰变量value和使用sun.misc.Unsafe来完成对value的原子操作。


AtomicBoolean的实现机制还是比较简单的,通过判断用volatile修改的value与1和0之间的关系来返回true和false,使用sun.misc.Unsafe来完成对value的原子操作

AtomicReference的实现机制就是通过sun.misc.Unsafe来对对象引用进行原子操作。

源码如下:

[java]   view plain  copy
  1. public class AtomicLong extends Number implements java.io.Serializable {  
  2.     private static final long serialVersionUID = 1927816293512124184L;  
  3.   
  4.       
  5.     private static final Unsafe unsafe = Unsafe.getUnsafe();  
  6.     private static final long valueOffset;  
  7.   
  8.     
  9.     static final boolean VM_SUPPORTS_LONG_CAS = VMSupportsCS8();  
  10.   
  11.     private static native boolean VMSupportsCS8();  
  12.   
  13.     static {  
  14.         try {  
  15.             valueOffset = unsafe.objectFieldOffset  
  16.                 (AtomicLong.class.getDeclaredField("value"));  
  17.         } catch (Exception ex) { throw new Error(ex); }  
  18.     }  
  19.   
  20.     private volatile long value;  
  21.   
  22.      
  23.     public AtomicLong(long initialValue) {  
  24.         value = initialValue;  
  25.     }  
  26.   
  27.     public AtomicLong() {  
  28.     }  
  29.   
  30.     public final long get() {  
  31.         return value;  
  32.     }  
  33.   
  34.     
  35.     public final void set(long newValue) {  
  36.         value = newValue;  
  37.     }  
  38.   
  39.     public final void lazySet(long newValue) {  
  40.         unsafe.putOrderedLong(this, valueOffset, newValue);  
  41.     }  
  42.   
  43.     public final long getAndSet(long newValue) {  
  44.         return unsafe.getAndSetLong(this, valueOffset, newValue);  
  45.     }  
  46.   
  47.     public final boolean compareAndSet(long expect, long update) {  
  48.         return unsafe.compareAndSwapLong(this, valueOffset, expect, update);  
  49.     }  
  50.   
  51.     public final boolean weakCompareAndSet(long expect, long update) {  
  52.         return unsafe.compareAndSwapLong(this, valueOffset, expect, update);  
  53.     }  
  54.   
  55.     public final long getAndIncrement() {  
  56.         return unsafe.getAndAddLong(this, valueOffset, 1L);  
  57.     }  
  58.   
  59.     public final long getAndDecrement() {  
  60.         return unsafe.getAndAddLong(this, valueOffset, -1L);  
  61.     }  
  62.   
  63.     public final long getAndAdd(long delta) {  
  64.         return unsafe.getAndAddLong(this, valueOffset, delta);  
  65.     }  
  66.   
  67.      
  68.     public final long incrementAndGet() {  
  69.         return unsafe.getAndAddLong(this, valueOffset, 1L) + 1L;  
  70.     }  
  71.   
  72.     public final long decrementAndGet() {  
  73.         return unsafe.getAndAddLong(this, valueOffset, -1L) - 1L;  
  74.     }  
  75.   
  76.     public final long addAndGet(long delta) {  
  77.         return unsafe.getAndAddLong(this, valueOffset, delta) + delta;  
  78.     }  
  79.   
  80.      
  81.     public final long getAndUpdate(LongUnaryOperator updateFunction) {  
  82.         long prev, next;  
  83.         do {  
  84.             prev = get();  
  85.             next = updateFunction.applyAsLong(prev);  
  86.         } while (!compareAndSet(prev, next));  
  87.         return prev;  
  88.     }  
  89.   
  90.     public final long updateAndGet(LongUnaryOperator updateFunction) {  
  91.         long prev, next;  
  92.         do {  
  93.             prev = get();  
  94.             next = updateFunction.applyAsLong(prev);  
  95.         } while (!compareAndSet(prev, next));  
  96.         return next;  
  97.     }  
  98.   
  99.     public final long getAndAccumulate(long x,  
  100.                                        LongBinaryOperator accumulatorFunction) {  
  101.         long prev, next;  
  102.         do {  
  103.             prev = get();  
  104.             next = accumulatorFunction.applyAsLong(prev, x);  
  105.         } while (!compareAndSet(prev, next));  
  106.         return prev;  
  107.     }  
  108.   
  109.     public final long accumulateAndGet(long x,  
  110.                                        LongBinaryOperator accumulatorFunction) {  
  111.         long prev, next;  
  112.         do {  
  113.             prev = get();  
  114.             next = accumulatorFunction.applyAsLong(prev, x);  
  115.         } while (!compareAndSet(prev, next));  
  116.         return next;  
  117.     }  
  118.   
  119.      
  120.     public String toString() {  
  121.         return Long.toString(get());  
  122.     }  
  123.   
  124.     public int intValue() {  
  125.         return (int)get();  
  126.     }  
  127.   
  128.     public long longValue() {  
  129.         return get();  
  130.     }  
  131.   
  132.     public float floatValue() {  
  133.         return (float)get();  
  134.     }  
  135.   
  136.     public double doubleValue() {  
  137.         return (double)get();  
  138.     }  
  139.   
  140. }  

AtomicBoolean的源码:

[java]   view plain  copy
  1. public class AtomicBoolean implements java.io.Serializable {  
  2.     private static final long serialVersionUID = 4654671469794556979L;  
  3.   
  4.     private static final Unsafe unsafe = Unsafe.getUnsafe();  
  5.     private static final long valueOffset;  
  6.   
  7.     static {  
  8.         try {  
  9.             valueOffset = unsafe.objectFieldOffset  
  10.                 (AtomicBoolean.class.getDeclaredField("value"));  
  11.         } catch (Exception ex) { throw new Error(ex); }  
  12.     }  
  13.   
  14.     private volatile int value;  
  15.   
  16.      
  17.     public AtomicBoolean(boolean initialValue) {  
  18.         value = initialValue ? 1 : 0;  
  19.     }  
  20.   
  21.     public AtomicBoolean() {  
  22.     }  
  23.   
  24.     public final boolean get() {  
  25.         return value != 0;  
  26.     }  
  27.   
  28.     public final boolean compareAndSet(boolean expect, boolean update) {  
  29.         int e = expect ? 1 : 0;  
  30.         int u = update ? 1 : 0;  
  31.         return unsafe.compareAndSwapInt(this, valueOffset, e, u);  
  32.     }  
  33.   
  34.      
  35.     public boolean weakCompareAndSet(boolean expect, boolean update) {  
  36.         int e = expect ? 1 : 0;  
  37.         int u = update ? 1 : 0;  
  38.         return unsafe.compareAndSwapInt(this, valueOffset, e, u);  
  39.     }  
  40.   
  41.     
  42.     public final void set(boolean newValue) {  
  43.         value = newValue ? 1 : 0;  
  44.     }  
  45.   
  46.     public final void lazySet(boolean newValue) {  
  47.         int v = newValue ? 1 : 0;  
  48.         unsafe.putOrderedInt(this, valueOffset, v);  
  49.     }  
  50.   
  51.     public final boolean getAndSet(boolean newValue) {  
  52.         boolean prev;  
  53.         do {  
  54.             prev = get();  
  55.         } while (!compareAndSet(prev, newValue));  
  56.         return prev;  
  57.     }  
  58.   
  59.     public String toString() {  
  60.         return Boolean.toString(get());  
  61.     }  
  62.   
  63. }  

AtomicReference源码:

[java]   view plain  copy
  1. public class AtomicReference<V> implements java.io.Serializable {  
  2.     private static final long serialVersionUID = -1848883965231344442L;  
  3.   
  4.     private static final Unsafe unsafe = Unsafe.getUnsafe();  
  5.     private static final long valueOffset;  
  6.   
  7.     static {  
  8.         try {  
  9.             valueOffset = unsafe.objectFieldOffset  
  10.                 (AtomicReference.class.getDeclaredField("value"));  
  11.         } catch (Exception ex) { throw new Error(ex); }  
  12.     }  
  13.   
  14.     private volatile V value;  
  15.   
  16.      
  17.     public AtomicReference(V initialValue) {  
  18.         value = initialValue;  
  19.     }  
  20.   
  21.     public AtomicReference() {  
  22.     }  
  23.   
  24.    
  25.     public final V get() {  
  26.         return value;  
  27.     }  
  28.   
  29.     public final void set(V newValue) {  
  30.         value = newValue;  
  31.     }  
  32.       
  33.     public final void lazySet(V newValue) {  
  34.         unsafe.putOrderedObject(this, valueOffset, newValue);  
  35.     }  
  36.   
  37.      
  38.     public final boolean compareAndSet(V expect, V update) {  
  39.         return unsafe.compareAndSwapObject(this, valueOffset, expect, update);  
  40.     }  
  41.   
  42.       
  43.     public final boolean weakCompareAndSet(V expect, V update) {  
  44.         return unsafe.compareAndSwapObject(this, valueOffset, expect, update);  
  45.     }  
  46.   
  47.     
  48.     @SuppressWarnings("unchecked")  
  49.     public final V getAndSet(V newValue) {  
  50.         return (V)unsafe.getAndSetObject(this, valueOffset, newValue);  
  51.     }  
  52.   
  53.     public final V getAndUpdate(UnaryOperator<V> updateFunction) {  
  54.         V prev, next;  
  55.         do {  
  56.             prev = get();  
  57.             next = updateFunction.apply(prev);  
  58.         } while (!compareAndSet(prev, next));  
  59.         return prev;  
  60.     }  
  61.   
  62.     public final V updateAndGet(UnaryOperator<V> updateFunction) {  
  63.         V prev, next;  
  64.         do {  
  65.             prev = get();  
  66.             next = updateFunction.apply(prev);  
  67.         } while (!compareAndSet(prev, next));  
  68.         return next;  
  69.     }  
  70.   
  71.     public final V getAndAccumulate(V x,  
  72.                                     BinaryOperator<V> accumulatorFunction) {  
  73.         V prev, next;  
  74.         do {  
  75.             prev = get();  
  76.             next = accumulatorFunction.apply(prev, x);  
  77.         } while (!compareAndSet(prev, next));  
  78.         return prev;  
  79.     }  
  80.   
  81.      
  82.     public final V accumulateAndGet(V x,  
  83.                                     BinaryOperator<V> accumulatorFunction) {  
  84.         V prev, next;  
  85.         do {  
  86.             prev = get();  
  87.             next = accumulatorFunction.apply(prev, x);  
  88.         } while (!compareAndSet(prev, next));  
  89.         return next;  
  90.     }  
  91.   
  92.       
  93.     public String toString() {  
  94.         return String.valueOf(get());  
  95.     }  
  96.   
  97. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值