java並發編程:線程安全管理類--原子操作類--AtomicReferenceArray


1.類 AtomicReferenceArray<E>

public class AtomicReferenceArray<E>extends Objectimplements Serializable

  可以用原子方式更新其元素的對象引用數組

2.構造函數詳解

AtomicReferenceArray

public AtomicReferenceArray(int length)
創建給定長度的新 AtomicReferenceArray。

 

參數:
length - 該數組的長度

AtomicReferenceArray

public AtomicReferenceArray(E[] array)
創建與給定數組具有相同長度的新 AtomicReferenceArray,並從給定數組復制其所有元素。

 

參數:
array - 從中復制元素的數組
拋出:
NullPointerException - 如果數組為 null

3.方法詳解

length

public final int length()
返回該數組的長度。

 

返回:
該數組的長度

get

public final E get(int i)
獲取位置 i 的當前值。

 

參數:
i - 索引
返回:
當前值

set

public final void set(int i,
                      E newValue)
將位置 i 的元素設置為給定值。

 

參數:
i - 索引
newValue - 新值

lazySet

public final void lazySet(int i,
                          E newValue)
最終將位置 i 的元素設置為給定值。

 

參數:
i - 索引
newValue - 新值
從以下版本開始:
1.6

getAndSet

public final E getAndSet(int i,
                         E newValue)
以原子方式將位置 i 的元素設置為給定值,並返回舊值。

 

參數:
i - 索引
newValue - 新值
返回:
以前的值

compareAndSet

public final boolean compareAndSet(int i,
                                   E expect,
                                   E update)
如果當前值 == 預期值,則以原子方式將位置 i 的元素設置為給定的更新值。

 

參數:
i - 索引
expect - 預期值
update - 新值
返回:
如果成功,則返回 true。返回 false 表示實際值與預期值不相等。

weakCompareAndSet

public final boolean weakCompareAndSet(int i,
                                       E expect,
                                       E update)
如果當前值 == 預期值,則以原子方式將位置 i 的元素設置為給定的更新值。

可能意外失敗並且不提供排序保證,因此幾乎只是 compareAndSet 的適當替換方法。

 

參數:
i - 索引
expect - 預期值
update - 新值
返回:
如果成功,則返回 true。

toString

public String toString()
返回數組當前值的字符串表示形式。

 

覆蓋:
Object 中的 toString
返回:
數組當前值的字符串表示形式。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM