Java Atomic的使用总结

来源:互联网 发布:淘宝食品代理公司注 编辑:程序博客网 时间:2024/04/30 07:44
众所周知volatile修饰的变量可以实现基本的加载和赋值的原子性,但是对于像i++等操作就不能保证原子性了,在JDK1.5之前我们只能通过synchronized(阻塞的方式)实现这些复合操作的原子性,在JDK1.5中java.util.concurrent.atomic包提供了若干个类能实现对int,long,boolean,reference的几个特殊方法非阻塞原子性,这一系列类的主要基于以下两点 

1.volatile修饰变量,保证get()/set()的原子性 
2.利用系统底层的CAS原语来实现非阻塞的其它方法原子操作 
  compareAndSwap(memorylocation,expectedValue,newValue);该操作接受一个预计值和新的赋值,当预计值与实际值相符合时,就表明该变量在此期间没有被别的线程改变(可能有ABA问题),就把新值赋给该引用. 

  通常将 CAS 用于同步的方式是从地址 V 读取值 A,执行多步计算来获得新值 B,然后使用 CAS 将 V 的值从 A 改为 B。如果 V 处的值尚未同时更改,则 CAS 操作成功


其中的类可以分成4组

  • AtomicBoolean,AtomicInteger,AtomicLong,AtomicReference
  • AtomicIntegerArray,AtomicLongArray
  • AtomicLongFieldUpdater,AtomicIntegerFieldUpdater,AtomicReferenceFieldUpdater
  • AtomicMarkableReference,AtomicStampedReference,AtomicReferenceArray
各个类的介绍 

1.AtomicInteger 
  实现了对Int的各种操作的原子化,我们看看其中的方法 
    
   //实现atomic类的最大诀窍##### 
    public final boolean compareAndSet(int expect, int update) { 
        //如果内存位置的值与期望值相同则,赋予新值,并返回true 
return unsafe.compareAndSwapInt(this, valueOffset, expect, update); 
    } 

   //该方法实现了i++的非阻塞的原子操作 
   public final int getAndIncrement() { 
        for (;;) { //循环,使用CAS的经典方式,这是实现non-blocking方式的代价 
            int current = get();//得到现在的值 
            int next = current + 1;//通过计算得到要赋予的新值 
            if (compareAndSet(current, next)) //关键点,调用CAS原子更新, 
                return current; 
        } 
    } 

   

2.AtmoicLong 
  与AtomicInteger相似,只是是基于Long 

3.AtmoicBoolean 
  与AtomimcInteger相似,基于boolean 
4.AtomicReference 
  与上述三个类不同,该类是用于原子地更新某个引用,只提供操作保证某个引用的更新会被原子化,常用封装某个引用会被多个线程频繁更新的场景,保证线程安全性 

public final V getAndSet(V newValue) { 
        while (true) { 
            V x = get(); 
            if (compareAndSet(x, newValue)) 
                return x; 
        } 
    } 


====================================================================== 

5.AtomicIntegerArray 
  对数组中的制定int提供几种特定的原子操作,注意不是对这个数组对象进行原子操作 
    //对数组的第i个元素进行原子的i--操作 
    //注意该类的原子操作都是针对数组中的某个指定元素的 
     public final int getAndDecrement(int i) { 
        while (true) { 
            int current = get(i); 
            int next = current - 1; 
            if (compareAndSet(i, current, next)) 
                return current; 
        } 
    } 

6.AtomicLongArray 
  与AtomicIntegerArray相似,基于long的 
7.AtomicReferenceArray 
  与AtomicIntegerArray相似,基于reference的 
====================================================================== 
8.AtomicLongFieldUpdater 
  上述的7个类都是基于对类自身的volatile数据提供原子操作,但是如何对已经存在的类中的volatile数据提供原子支持呢,这个需求是很常见的,因为我们在日常开发中经常要使用第三方的class.8#9#10这三个类就是利用反射机制对指定类的指定的volatile field提供原子操作的工具类.这三个类在原子数据结构中被用到,可以参考源码.注意只对publie volatile的非static 成员起作用 
//工厂方法,给指定类的指定fild(必须是long)建立一个update,field必须是volatile的 
public static <U> AtomicLongFieldUpdater<U> newUpdater(Class<U> tclass, String fieldName) 

9.AtomicIntegerFieldUpdater 
与上述类相似,只是基于int 
10.AtomicReferenceFieldUpdater 
  基于Reference 
====================================================================== 
11.AtomicMarkableReference 
12.AtomicStampedReference

原创粉丝点击