AtomicReference 原子引用
生活随笔
收集整理的這篇文章主要介紹了
AtomicReference 原子引用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?
?
?
原創(chuàng)?2016年03月04日 13:35:201.簡介
賦值操作不是線程安全的。若想不用鎖來實現(xiàn),可以用AtomicReference<V>這個類,實現(xiàn)對象引用的原子更新。
使用場景:一個線程使用student對象,另一個線程負責定時讀表,更新這個對象。那么就可以用AtomicReference這個類。java.util.concurrent.atomic.AtomicReference<V>
類的名字。
2.常用方法
java.util.concurrent.atomic.AtomicReference.AtomicReference(V initialValue)構(gòu)造函數(shù)。
V java.util.concurrent.atomic.AtomicReference.get()
返回當前的引用。
boolean java.util.concurrent.atomic.AtomicReference.compareAndSet(V expect, V update)
如果當前值與給定的expect相等,(注意是引用相等而不是equals()相等),更新為指定的update值。
V java.util.concurrent.atomic.AtomicReference.getAndSet(V newValue)
原子地設為給定值并返回舊值。
void java.util.concurrent.atomic.AtomicReference.set(V newValue)
注意此方法不是原子的。不明白為什么要提供這個方法,很容易誤用。
轉(zhuǎn)載于:https://www.cnblogs.com/ydxblog/p/7988487.html
總結(jié)
以上是生活随笔為你收集整理的AtomicReference 原子引用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 主从同步-读写分离
- 下一篇: iOS14.2 Beta 1更新内容及升