這一節我們先來看一下基本類型: AtomicInteger, AtomicLong, AtomicBoolean。AtomicInteger和AtomicLong的使用方法差不多,AtomicBoolean因為比較簡單所以方法比前兩個都少,那我們這節主要挑AtomicLong來說,會使用一個,其餘的大同小異。
1.原子操作與一般操作異同
我們在說原子操作之前為了有個對比為什麼需要這些原子類而不是普通的基本數據類型就能滿足我們的使用要求,那就不得不提原子操作不同的地方。
當你在操作一個普通變量時,你在Java實現的每個操作,在程序編譯時會被轉換成幾個機器能讀懂的指令。例如,當你分配一個值給變量,在Java你只使用了一個指令,但是當你編譯這個程序時,這個指令就被轉換成多個JVM 語言指令。這樣子的話當你在操作多個線程且共享一個變量時,就會導致數據不一致的錯誤。
為了避免這樣的問題,Java引入了原子變量。當一個線程正在操作一個原子變量時,即使其他線程也想要操作這個變量,類的實現中含有一個檢查那步驟操作是否完成的機制。 基本上,操作獲取變量的值,改變本地變量值,然後嘗試以新值代替舊值。如果舊值還是一樣,那麼就改變它。如果不一樣,方法再次開始操作。這個操作稱為 Compare and Set(簡稱CAS,比較並交換的意思)。
原子變量不使用任何鎖或者其他同步機制來保護它們的值的訪問。他們的全部操作都是基於CAS操作。它保證幾個線程可以同時操作一個原子對象也不會出現數據不一致的錯誤,並且它的性能比使用受同步機制保護的正常變量要好。
2.AtomicLong簡介
由字面意義我們可以知道AtomicLong可以用原子方式更新的 long 值,下面我們看一下他的構造方法和一般方法:
構造方法:
AtomicLong() //創建具有初始值 0 的新 AtomicLong。
AtomicLong(long initialValue) //創建具有給定初始值的新 AtomicLong。方法:
long addAndGet(long delta) //以原子方式將給定值添加到當前值。
boolean compareAndSet(long expect, long update) //如果當前值 == 預期值,則以原子方式將該值 設置為給定的更新值。
long decrementAndGet() //以原子方式將當前值減 1。
double doubleValue() //以 double 形式返回指定的數值。
float floatValue() //以 float 形式返回指定的數值。
long get() //獲取當前值。
long getAndAdd(long delta) //以原子方式將給定值添加到當前值。
long getAndDecrement() //以原子方式將當前值減 1。
long getAndIncrement() //以原子方式將當前值加 1。
long getAndSet(long newValue)// 以原子方式設置為給定值,並返回舊值。
long incrementAndGet() //以原子方式將當前值加 1。
int intValue() // 以 int 形式返回指定的數值。
void lazySet(long newValue) //最後設置為給定值。
long longValue() // 以 long 形式返回指定的數值。
void set(long newValue) //設置為給定值。
String toString() // 返回當前值的字符串表示形式。
boolean weakCompareAndSet(long expect, long update) //如果當前值 == 預期值,則以原子方式將該值設置為給定的更新值。
3.使用AtomicLong
3.1 創建AtomicLong
創建AtomicLong的過程如下:
AtomicLong atomicLong = new AtomicLong();
此示例創建一個初始值為0的AtomicLong 。
如果你想創建一個帶有初始值的AtomicLong ,你可以這樣做:
AtomicLong atomicLong = new AtomicLong(123);
此示例將值123作為參數傳遞給AtomicLong裝訂器,該裝置將AtomicLong實例的初始值設置為123 。
3.2 獲取AtomicLong值
您可以通過get()方法get() AtomicLong實例的值。 這裡是一個AtomicLong.get()示例:
AtomicLong atomicLong = new AtomicLong(123);
long theValue = atomicLong.get();
設置AtomicLong值
您可以通過set()方法set() AtomicLong實例的值。 這裡是一個AtomicLong.set()示例:
AtomicLong atomicLong = new AtomicLong(123); atomicLong.set(234);
此示例創建一個初始值為123的AtomicLong示例,然後在下一行中將其值設置為234 。
3.3 比較並設置AtomicLong值
AtomicLong類也有一個原子compareAndSet()方法。 此方法將AtomicLong實例的當前值與AtomicLong進行比較,如果這兩個值相等, AtomicLong實例設置新值。 這裡是一個AtomicLong.compareAndSet()示例:
AtomicLong atomicLong = new AtomicLong(123); long expectedValue = 123; long newValue = 234; atomicLong.compareAndSet(expectedValue,newValue);
此示例首先創建一個初始值為123的AtomicLong實例。 然後,它將AtomicLong的值與期望值123進行比較,如果它們相等,則AtomicLong的新值變為234 ;
3.4 添加到AtomicLong值
AtomicLong類包含幾個方法,您可以使用這些方法向AtomicLong添加值並返回其值。這裡我們要重點關注一下,因為這幾個方法會如果我們使用不當會造成歧義。 這些方法是:
addAndGet()
getAndAdd()
getAndIncrement()
incrementAndGet()
第一種方法addAndGet()向AtomicLong添加一個數字,並在添加後返回其值。
第二種方法getAndAdd()還向AtomicLong添加一個數字,但返回AtomicLong在添加值之前的值。
您應該使用這兩種方法中的哪一種取決於您的用例。 這裡有兩個例子:
AtomicLong atomicLong = new AtomicLong(); System.out.println(atomicLong.getAndAdd(10)); System.out.println(atomicLong.addAndGet(10));
此示例將打印出值0和20 。 首先,示例在添加10之前獲取AtomicLong的值。 它的值在加法之前為0.然後示例將10添加到AtomicLong ,並獲取添加後的值。 該值現在為20。
您也可以通過這兩種方法向AtomicLong添加負數。 結果實際上是一個減法。
方法getAndIncrement()和incrementAndGet()工作原理像getAndAdd()和addAndGet()但只是添加1到AtomicLong的值。
3.5 從AtomicLong值中減去
AtomicLong類還包含一些用於從AtomicLong值中以AtomicLong值的方法。 這些方法是:
decrementAndGet()
getAndDecrement()
decrementAndGet()從AtomicLong值中減去1,並在AtomicLong後返回其值。 getAndDecrement()也從AtomicLong值中減去1,但返回AtomicLong在AtomicLong之前的值。
由上我們大致知道了AtomicLong的用法,AtomicBoolean,AtomicInteger也與它的用法差不多,我們看一下API他們各自的方法就知道該如何使用。
[月球人 ] java併發編程專題(十)----(JUC原子類)基本類型詳解已經有252次圍觀