第十一章 AtomicInteger源碼解析,atomicinteger源碼

來源:互聯網
上載者:User

第十一章 AtomicInteger源碼解析,atomicinteger源碼

1、原子類

  • 可以實現一些原子操作
  • 基於CAS

下面就以AtomicInteger為例。

 

2、AtomicInteger

在沒有AtomicInteger之前,對於一個Integer的安全執行緒操作,是需要使用同步鎖來實現的,當然現在也可以通過ReentrantLock來實現,但是最好最方便的實現方式是採用AtomicInteger。

具體樣本:

package com.collection.test;import java.util.concurrent.atomic.AtomicInteger;/** * 原子類的測試 */public class AtomicTest { private static AtomicInteger atomicInteger = new AtomicInteger(); //擷取當前值 public static void getCurrentValue(){ System.out.println(atomicInteger.get());//-->0 } //設定value值 public static void setValue(){ atomicInteger.set(12);//直接用12覆蓋舊值 System.out.println(atomicInteger.get());//-->12 } //根據方法名稱getAndSet就知道先get,則最後返回的就是舊值,如果get在後,就是返回新值 public static void getAndSet(){ System.out.println(atomicInteger.getAndSet(15));//-->12 } public static void getAndIncrement(){ System.out.println(atomicInteger.getAndIncrement());//-->15 } public static void getAndDecrement(){ System.out.println(atomicInteger.getAndDecrement());//-->16 } public static void getAndAdd(){ System.out.println(atomicInteger.getAndAdd(10));//-->15 } public static void incrementAndGet(){ System.out.println(atomicInteger.incrementAndGet());//-->26 } public static void decrementAndGet(){ System.out.println(atomicInteger.decrementAndGet());//-->25 } public static void addAndGet(){ System.out.println(atomicInteger.addAndGet(20));//-->45 } public static void main(String[] args) { AtomicTest test = new AtomicTest(); test.getCurrentValue(); test.setValue(); //返回舊值系列 test.getAndSet(); test.getAndIncrement(); test.getAndDecrement(); test.getAndAdd(); //返回新值系列 test.incrementAndGet(); test.decrementAndGet(); test.addAndGet(); }}View Code

原始碼:

private volatile int value;// 初始化值 /** * 建立一個AtomicInteger,初始值value為initialValue */ public AtomicInteger(int initialValue) { value = initialValue; } /** * 建立一個AtomicInteger,初始值value為0 */ public AtomicInteger() { } /** * 返回value */ public final int get() { return value; } /** * 為value設值(基於value),而其他動作是基於舊值<--get() */ public final void set(int newValue) { value = newValue; } public final boolean compareAndSet(int expect, int update) { return unsafe.compareAndSwapInt(this, valueOffset, expect, update); } /** * 基於CAS為舊值設定新值,採用無限迴圈,直到設定成功為止 * * @return 返回舊值 */ public final int getAndSet(int newValue) { for (;;) { int current = get();// 擷取當前值(舊值) if (compareAndSet(current, newValue))// CAS新值替代舊值 return current;// 返回舊值 } } /** * 當前值+1,採用無限迴圈,直到+1成功為止 * @return the previous value 返回舊值 */ public final int getAndIncrement() { for (;;) { int current = get();//擷取當前值 int next = current + 1;//當前值+1 if (compareAndSet(current, next))//基於CAS賦值 return current; } } /** * 當前值-1,採用無限迴圈,直到-1成功為止 * @return the previous value 返回舊值 */ public final int getAndDecrement() { for (;;) { int current = get(); int next = current - 1; if (compareAndSet(current, next)) return current; } } /** * 當前值+delta,採用無限迴圈,直到+delta成功為止 * @return the previous value 返回舊值 */ public final int getAndAdd(int delta) { for (;;) { int current = get(); int next = current + delta; if (compareAndSet(current, next)) return current; } } /** * 當前值+1, 採用無限迴圈,直到+1成功為止 * @return the updated value 返回新值 */ public final int incrementAndGet() { for (;;) { int current = get(); int next = current + 1; if (compareAndSet(current, next)) return next;//返回新值 } } /** * 當前值-1, 採用無限迴圈,直到-1成功為止 * @return the updated value 返回新值 */ public final int decrementAndGet() { for (;;) { int current = get(); int next = current - 1; if (compareAndSet(current, next)) return next;//返回新值 } } /** * 當前值+delta,採用無限迴圈,直到+delta成功為止 * @return the updated value 返回新值 */ public final int addAndGet(int delta) { for (;;) { int current = get(); int next = current + delta; if (compareAndSet(current, next)) return next;//返回新值 } } /** * 擷取當前值 */ public int intValue() { return get(); }View Code

說明:使用與原始碼都簡單到爆了!自己看看注釋。

注意:

  • value是volatile的,關於volatile的相關內容見《附2 volatile》,具體連結:http://www.cnblogs.com/java-zhao/p/5125698.html
  • 單步操作:例如set()是直接對value進行操作的,不需要CAS,因為單步操作就是原子操作。
  • 多步操作:例如getAndSet(int newValue)是兩步操作-->先擷取值,在設定值,所以需要原子化,這裡採用CAS實現。
  • 對於方法是返回舊值還是新值,直接看方法是以get開頭(返回舊值)還是get結尾(返回新值)就好
  • CAS:比較CPU記憶體上的值是不是當前值current,如果是就換成新值update,如果不是,說明擷取值之後到設定值之前,該值已經被別人先一步設定過了,此時如果自己再設定值的話,需要在別人修改後的值的基礎上去操作,否則就會覆蓋別人的修改,所以這個時候會直接返回false,再進行無限迴圈,重新擷取當前值,然後再基於CAS進行加減操作。
  • 如果還是不懂CAS,類比資料庫的樂觀鎖。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.