JUC原子類2-atomicLong原子類

概要
app

AtomicInteger,AtomicLong和AtomicBoolean這三個基本類型的原子類的原理和用法類似。本文章以AtomicLong對基本類型的原子類進行介紹函數

AtomicLong介紹和函數列表性能

AtomicLong的做用是對長整型進行原子操做。在32位操做系統中,64位的long和double變量因爲會被JVM看成兩個分離的32位來進行操做,因此不具備原子性。而使用AtomicLong能讓long的操做保持原子性。this

函數列表:spa

// 構造函數
AtomicLong()
// 建立值爲initialValue的AtomicLong對象
AtomicLong(long initialValue)
// 以原子方式設置當前值爲newValue。
final void set(long newValue) 
// 獲取當前值
final long get() 
// 以原子方式將當前值減 1,並返回減1後的值。等價於「--num」
final long decrementAndGet() 
// 以原子方式將當前值減 1,並返回減1前的值。等價於「num--」
final long getAndDecrement() 
// 以原子方式將當前值加 1,並返回加1後的值。等價於「++num」
final long incrementAndGet() 
// 以原子方式將當前值加 1,並返回加1前的值。等價於「num++」
final long getAndIncrement()    
// 以原子方式將delta與當前值相加,並返回相加後的值。
final long addAndGet(long delta) 
// 以原子方式將delta添加到當前值,並返回相加前的值。
final long getAndAdd(long delta) 
// 若是當前值 == expect,則以原子方式將該值設置爲update。成功返回true,不然返回false,而且不修改原值。
final boolean compareAndSet(long expect, long update)
// 以原子方式設置當前值爲newValue,並返回舊值。
final long getAndSet(long newValue)
// 返回當前值對應的int值
int intValue() 
// 獲取當前值對應的long值
long longValue()    
// 以 float 形式返回當前值
float floatValue()    
// 以 double 形式返回當前值
double doubleValue()    
// 最後設置爲給定值。延時設置變量值,這個等價於set()方法,可是因爲字段是volatile類型的,所以次字段的修改會比普通字段(非volatile字段)有稍微的性能延時(儘管能夠忽略),因此若是不是想當即讀取設置的新值,容許在「後臺」修改值,那麼此方法就頗有用。若是仍是難以理解,這裏就相似於啓動一個後臺線程如執行修改新值的任務,原線程就不等待修改結果當即返回(這種解釋實際上是不正確的,可是能夠這麼理解)。
final void lazySet(long newValue)
// 若是當前值 == 預期值,則以原子方式將該設置爲給定的更新值。JSR規範中說:以原子方式讀取和有條件地寫入變量但不 建立任何 happen-before 排序,所以不提供與除 weakCompareAndSet 目標外任何變量之前或後續讀取或寫入操做有關的任何保證。大意就是說調用weakCompareAndSet時並不能保證不存在happen-before的發生(也就是可能存在指令重排序致使此操做失敗)。可是從Java源碼來看,其實此方法並無實現JSR規範的要求,最後效果和compareAndSet是等效的,都調用了unsafe.compareAndSwapInt()完成操做。
final boolean weakCompareAndSet(long expect, long update)

incrementAndGet()的源碼以下:操作系統

public final long incrementAndGet() {
    for (;;) {
        // 獲取AtomicLong當前對應的long值
        long current = get();
        // 將current加1
        long next = current + 1;
        // 經過CAS函數,更新current的值
        if (compareAndSet(current, next))
            return next;
    }
}

說明:線程

(01)incrementAndGet()首先會根據get()獲取AtomicLong對應的long值。該值是volatile類型的變量,get()的源碼以下:code

// value是AtomicLong對應的long值
private volatile long value;
// 返回AtomicLong對應的long值
public final long get() {
    return value;
}

(02)incrementAndGet()接着將current加一,而後經過CAS函數,將新的值賦值給value對象

CAS的源碼爲:blog

public final boolean compareAndSet(long expect, long update) {
    return unsafe.compareAndSwapLong(this, valueOffset, expect, update);
}

CAS的做用是更新AtomicLong對應的long值。它會比較AtomicLong的原始值是否與expect相等,若相等的話,則設置AtomicLong的值爲update

AtomicLong示例

 

public class Main
{
    public static void main(String[] args)
    {
        AtomicLong mAtoLong = new AtomicLong();

        mAtoLong.set(0x0123456789ABCDEFL);
        System.out.printf("%20s : 0x%016X\n", "get()", mAtoLong.get());
        System.out.printf("%20s : 0x%016X\n", "intValue()", mAtoLong.intValue());
        System.out.printf("%20s : 0x%016X\n", "longValue()", mAtoLong.longValue());
        System.out.printf("%20s : %s\n", "doubleValue()", mAtoLong.doubleValue());
        System.out.printf("%20s : %s\n", "floatValue()", mAtoLong.floatValue());

        System.out.printf("%20s : 0x%016X\n", "getAndDecrement()", mAtoLong.getAndDecrement());
        System.out.printf("%20s : 0x%016X\n", "decrementAndGet()", mAtoLong.decrementAndGet());
        System.out.printf("%20s : 0x%016X\n", "getAndIncrement()", mAtoLong.getAndIncrement());
        System.out.printf("%20s : 0x%016X\n", "incrementAndGet()", mAtoLong.incrementAndGet());

        System.out.printf("%20s : 0x%016X\n", "addAndGet(0x10)", mAtoLong.addAndGet(0x10));
        System.out.printf("%20s : 0x%016X\n", "getAndAdd(0x10)", mAtoLong.getAndAdd(0x10));

        System.out.printf("\n%20s : 0x%016X\n", "get()", mAtoLong.get());

        System.out.printf("%20s : %s\n", "compareAndSet()", mAtoLong.compareAndSet(0x12345679L, 0xFEDCBA9876543210L));
        System.out.printf("%20s : 0x%016X\n", "get()", mAtoLong.get());
    }
}

 

運行結果:

   get() : 0x0123456789ABCDEF
          intValue() : 0x0000000089ABCDEF
         longValue() : 0x0123456789ABCDEF
       doubleValue() : 8.1985529216486896E16
        floatValue() : 8.1985531E16
   getAndDecrement() : 0x0123456789ABCDEF
   decrementAndGet() : 0x0123456789ABCDED
   getAndIncrement() : 0x0123456789ABCDED
   incrementAndGet() : 0x0123456789ABCDEF
     addAndGet(0x10) : 0x0123456789ABCDFF
     getAndAdd(0x10) : 0x0123456789ABCDFF

               get() : 0x0123456789ABCE0F
     compareAndSet() : false
相關文章
相關標籤/搜索