Java AtomicInteger的用法

1.java.util.concurrent.atomic 的包裏有AtomicBoolean, AtomicInteger,AtomicLong,AtomicLongArray,
AtomicReference等原子類的類,主要用於在高併發環境下的高效程序處理,來幫助咱們簡化同步處理.java

在Java語言中,++i和i++操做並非線程安全的,在使用的時候,不可避免的會用到synchronized關鍵字。而AtomicInteger則經過一種線程安全的加減操做接口。算法

2.AtomicInteger的基本方法安全

  • 建立一個AtomicInteger
AtomicInteger atomicInteger = new AtomicInteger(123);
  System.out.println(atomicInteger.get());


--->輸出 : 123
  • 建立一個不傳值的,默認值爲0
AtomicInteger atomicInteger = new AtomicInteger();
  System.out.println(atomicInteger.get());



---->輸出: 0
  • 獲取和賦值
atomicInteger.get(); //獲取當前值
atomicInteger.set(999); //設置當前值

atomicInteger.compareAndSet(expectedValue,newValue)併發

public static void main(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(0);
        System.out.println(atomicInteger.get());

        int expectedValue = 123;
        int newValue      = 234;
        Boolean b =atomicInteger.compareAndSet(expectedValue, newValue);
        System.out.println(b);
        System.out.println(atomicInteger);

    }

----》輸出結果爲: 0 false 0


 public static void main(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(123);
        System.out.println(atomicInteger.get());

        int expectedValue = 123;
        int newValue      = 234;
        Boolean b =atomicInteger.compareAndSet(expectedValue, newValue);
        System.out.println(b);
        System.out.println(atomicInteger);

    }

-----》輸出結果爲: 123 true  234

由上可知該方法表示,atomicInteger的值與expectedValue相比較,若是不相等,則返回false,
atomicInteger原有值保持不變;若是二者相等,則返回true,atomicInteger的值更新爲newValue

 

  • getAndAdd()方法與AddAndGet方法
AtomicInteger atomicInteger = new AtomicInteger(123);
        System.out.println(atomicInteger.get());  --123

        System.out.println(atomicInteger.getAndAdd(10)); --123 獲取當前值,並加10
        System.out.println(atomicInteger.get()); --133


        System.out.println(atomicInteger.addAndGet(10)); --143 獲取加10後的值,先加10
        System.out.println(atomicInteger.get()); --143

 getAndDecrement()和DecrementAndGet()方法app

AtomicInteger atomicInteger = new AtomicInteger(123);
        System.out.println(atomicInteger.get());   --123

        System.out.println(atomicInteger.getAndDecrement()); --123 獲取當前值並自減
        System.out.println(atomicInteger.get());  --122


        System.out.println(atomicInteger.decrementAndGet()); --121 先自減再獲取減1後的值
        System.out.println(atomicInteger.get()); --121

3.使用AtomicInteger,即便不用同步塊synchronized,最後的結果也是100,可用看出AtomicInteger的做用,用原子方式更新的int值。主要用於在高併發環境下的高效程序處理。使用非阻塞算法來實現併發控制。ide

public class Counter {

    public static AtomicInteger count = new AtomicInteger(0);

    public static void inc(){
        try{
            Thread.sleep(1); //延遲1毫秒

        }catch (InterruptedException e){ //catch住中斷異常,防止程序中斷
            e.printStackTrace();

        }
        count.getAndIncrement();//count值自加1
    }


    public static void main(String[] args) throws InterruptedException {


        final CountDownLatch latch = new CountDownLatch(100);

        for(int i=0;i<100;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Counter.inc();
                    latch.countDown();
                }
            }).start();
        }
        latch.await();

        System.out.println("運行結果:"+Counter.count);


    }
}

運行結果: 100

4.使用普通Integer高併發

public class Counter {

    public volatile  static int count = 0;

    public static void inc(){
        try{
            Thread.sleep(1); //延遲1毫秒

        }catch (InterruptedException e){ //catch住中斷異常,防止程序中斷
            e.printStackTrace();

        }
        count++;//count值自加1
    }


    public static void main(String[] args) throws InterruptedException {


        final CountDownLatch latch = new CountDownLatch(100);

        for(int i=0;i<100;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Counter.inc();
                    latch.countDown();
                }
            }).start();
        }
        latch.await();

        System.out.println("運行結果:"+Counter.count);


    }
}

運行結果:98

5.若是在inc方法前面加個synchronized也能是線程安全的;工具

它用來修飾一個方法或者一個代碼塊的時候,可以保證在同一時刻最多隻有一個線程執行該段代碼。this

import java.util.concurrent.CountDownLatch;

/**
 * created by guanguan  on 2017/10/23
 **/
public class Counter {

     public volatile static  Integer count = 0;

    public synchronized static void inc(){
        try{
            Thread.sleep(1); //延遲1毫秒

        }catch (InterruptedException e){ //catch住中斷異常,防止程序中斷
            e.printStackTrace();

        }
          count++;//count值自加1
    }


    public static void main(String[] args) throws InterruptedException {


        final CountDownLatch latch = new CountDownLatch(100);

        for(int i=0;i<100;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Counter.inc();
                    latch.countDown();
                }
            }).start();
        }
        latch.await();

        System.out.println("運行結果:"+Counter.count);


    }
}

運行結果:100

 

 

synchronized的使用說明:atom

1、當兩個併發線程訪問同一個對象object中的這個synchronized(this)同步代碼塊時,一個時間內只能有一個線程獲得執行。另外一個線程必須等待當前線程執行完這個代碼塊之後才能執行該代碼塊。

     2、然而,當一個線程訪問object的一個synchronized(this)同步代碼塊時,另外一個線程仍然能夠訪問該object中的非synchronized(this)同步代碼塊。

     3、尤爲關鍵的是,當一個線程訪問object的一個synchronized(this)同步代碼塊時,其餘線程對object中全部其它synchronized(this)同步代碼塊的訪問將被阻塞。

     4、第三個例子一樣適用其它同步代碼塊。也就是說,當一個線程訪問object的一個synchronized(this)同步代碼塊時,它就得到了這個object的對象鎖。結果,其它線程對該object對象全部同步代碼部分的訪問都被暫時阻塞。

     5、以上規則對其它對象鎖一樣適用.

6.從上面的例子中咱們能夠看出:使用AtomicInteger是很是的安全的.並且由於AtomicInteger由硬件提供原子操做指令實現的。在非激烈競爭的狀況下,開銷更小,速度更快。

java的關鍵域有3個
// setup to use Unsafe.compareAndSwapInt for updates  
private static final Unsafe unsafe = Unsafe.getUnsafe();  
private static final long valueOffset;  
private volatile int value;

 這裏, unsafe是java提供的得到對對象內存地址訪問的類,註釋已經清楚的寫出了,它的做用就是在更新操做時提供「比較並替換」的做用。實際上就是AtomicInteger中的一個工具。

valueOffset是用來記錄value自己在內存的便宜地址的,這個記錄,也主要是爲了在更新操做在內存中找到value的位置,方便比較。

注意:value是用來存儲整數的時間變量,這裏被聲明爲volatile,就是爲了保證在更新操做時,當前線程能夠拿到value最新的值(併發環境下,value可能已經被其餘線程更新了)。

這裏,咱們以自增的代碼爲例,能夠看到這個併發控制的核心算法:

源碼

 public final int updateAndGet(IntUnaryOperator updateFunction) {
        int prev, next;
        do {
            prev = get();
            next = updateFunction.applyAsInt(prev);
        } while (!compareAndSet(prev, next));
        return next;
    }
相關文章
相關標籤/搜索