日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java多线程--AtomicReference

發布時間:2025/3/18 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java多线程--AtomicReference 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

AtomicReference介紹

AtomicReference是作用是對"對象"進行原子操作。

AtomicReference源碼分析(基于JDK1.7.0_40)

在JDK1.7.0_40中AtomicReference.java的源碼如下:

[java]?view plaincopy
  • public?class?AtomicReference<V>??implements?java.io.Serializable?{??
  • ????private?static?final?long?serialVersionUID?=?-1848883965231344442L;??
  • ??
  • ????//?獲取Unsafe對象,Unsafe的作用是提供CAS操作??
  • ????private?static?final?Unsafe?unsafe?=?Unsafe.getUnsafe();??
  • ????private?static?final?long?valueOffset;??
  • ??
  • ????static?{??
  • ??????try?{??
  • ????????valueOffset?=?unsafe.objectFieldOffset??
  • ????????????(AtomicReference.class.getDeclaredField("value"));??
  • ??????}?catch?(Exception?ex)?{?throw?new?Error(ex);?}??
  • ????}??
  • ??
  • ????//?volatile類型??
  • ????private?volatile?V?value;??
  • ??
  • ????public?AtomicReference(V?initialValue)?{??
  • ????????value?=?initialValue;??
  • ????}??
  • ??
  • ????public?AtomicReference()?{??
  • ????}??
  • ??
  • ????public?final?V?get()?{??
  • ????????return?value;??
  • ????}??
  • ??
  • ????public?final?void?set(V?newValue)?{??
  • ????????value?=?newValue;??
  • ????}??
  • ??
  • ????public?final?void?lazySet(V?newValue)?{??
  • ????????unsafe.putOrderedObject(this,?valueOffset,?newValue);??
  • ????}??
  • ??
  • ????public?final?boolean?compareAndSet(V?expect,?V?update)?{??
  • ????????return?unsafe.compareAndSwapObject(this,?valueOffset,?expect,?update);??
  • ????}??
  • ??
  • ????public?final?boolean?weakCompareAndSet(V?expect,?V?update)?{??
  • ????????return?unsafe.compareAndSwapObject(this,?valueOffset,?expect,?update);??
  • ????}??
  • ??
  • ????public?final?V?getAndSet(V?newValue)?{??
  • ????????while?(true)?{??
  • ????????????V?x?=?get();??
  • ????????????if?(compareAndSet(x,?newValue))??
  • ????????????????return?x;??
  • ????????}??
  • ????}??
  • ??
  • ????public?String?toString()?{??
  • ????????return?String.valueOf(get());??
  • ????}??
  • }<span?style="font-family:?'Courier?New'?!important;?font-size:?12px?!important;?line-height:?1.5?!important;?color:?rgb(0,?0,?0);"></span>??
  • 說明:
    AtomicReference的源碼比較簡單。它是通過"volatile"和"Unsafe提供的CAS函數實現"原子操作。
    (01) value是volatile類型。這保證了:當某線程修改value的值時,其他線程看到的value值都是最新的value值,即修改之后的volatile的值。
    (02) 通過CAS設置value。這保證了:當某線程池通過CAS函數(如compareAndSet函數)設置value時,它的操作是原子的,即線程在操作value時不會被中斷。

    ?

    AtomicReference示例

    [java]?view plaincopy
  • //?AtomicReferenceTest.java的源碼??
  • import?java.util.concurrent.atomic.AtomicReference;??
  • ??
  • public?class?AtomicReferenceTest?{??
  • ??????
  • ????public?static?void?main(String[]?args){??
  • ??
  • ????????//?創建兩個Person對象,它們的id分別是101和102。??
  • ????????Person?p1?=?new?Person(101);??
  • ????????Person?p2?=?new?Person(102);??
  • ????????//?新建AtomicReference對象,初始化它的值為p1對象??
  • ????????AtomicReference?ar?=?new?AtomicReference(p1);??
  • ????????//?通過CAS設置ar。如果ar的值為p1的話,則將其設置為p2。??
  • ????????ar.compareAndSet(p1,?p2);??
  • ??
  • ????????Person?p3?=?(Person)ar.get();??
  • ????????System.out.println("p3?is?"+p3);??
  • ????????System.out.println("p3.equals(p1)="+p3.equals(p1));??
  • ????}??
  • }??
  • ??
  • class?Person?{??
  • ????volatile?long?id;??
  • ????public?Person(long?id)?{??
  • ????????this.id?=?id;??
  • ????}??
  • ????public?String?toString()?{??
  • ????????return?"id:"+id;??
  • ????}??
  • }<span?style="font-family:?'Courier?New'?!important;?font-size:?12px?!important;?line-height:?1.5?!important;?color:?rgb(0,?0,?0);"></span>??
  • 運行結果:

    [java]
  • p3?is?id:102??
  • p3.equals(p1)=false<span?style="font-family:?'Courier?New'?!important;?font-size:?12px?!important;?line-height:?1.5?!important;?color:?rgb(0,?0,?255);"></span>??
  • 結果說明:
    新建AtomicReference對象ar時,將它初始化為p1。
    緊接著,通過CAS函數對它進行設置。如果ar的值為p1的話,則將其設置為p2。
    最后,獲取ar對應的對象,并打印結果。p3.equals(p1)的結果為false,這是因為Person并沒有覆蓋equals()方法,而是采用繼承自Object.java的equals()方法;而Object.java中的equals()實際上是調用"=="去比較兩個對象,即比較兩個對象的地址是否相等。

    總結

    以上是生活随笔為你收集整理的java多线程--AtomicReference的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。