併發程式設計-concurrent指南-原子操作類-AtomicInteger
在java併發程式設計中,會出現++,--等操作,但是這些不是原子性操作,這線上程安全上面就會出現相應的問題。因此java提供了相應類的原子性操作類。
1.AtomicInteger
可以用原子方式更新的 int
值。有關原子變數屬性的描述,請參閱 java.util.concurrent.atomic
包規範。AtomicInteger
可用在應用程式中(如以原子方式增加的計數器),並且不能用於替換 Integer
。但是,此類確實擴充套件了 Number
,允許那些處理基於數字類的工具和實用工具進行統一訪問。
構造方法:
1.AtomicInteger
public AtomicInteger(int initialValue)
-
建立具有給定初始值的新 AtomicInteger
- 引數:
-
initialValue
- 初始值
2.AtomicInteger
public AtomicInteger()
建立具有初始值 0
的新 AtomicInteger。
方法詳解:
get
public final int get()
-
獲取當前值。
-
- 返回:
- 當前值
set
public final void set(int newValue)
-
設定為給定值。
-
- 引數:
-
newValue
- 新值
lazySet
public final void lazySet(int newValue)
-
最後設定為給定值。
-
- 引數:
-
newValue
- 新值 - 從以下版本開始:
- 1.6
getAndSet
public final int getAndSet(int newValue)
-
以原子方式設定為給定值,並返回舊值。
-
- 引數:
-
newValue
- 新值 - 返回:
- 以前的值
compareAndSet
public final boolean compareAndSet(int expect, int update)
-
如果當前值
==
預期值,則以原子方式將該值設定為給定的更新值。 -
- 引數:
-
expect
- 預期值 -
update
- 新值 - 返回:
- 如果成功,則返回 true。返回 False 指示實際值與預期值不相等。
weakCompareAndSet
public final boolean weakCompareAndSet(int expect, int update)
-
如果當前值
==
預期值,則以原子方式將該設定為給定的更新值。可能意外失敗並且不提供排序保證,所以只有在很少的情況下才對
compareAndSet
進行適當地選擇。 -
- 引數:
-
expect
- 預期值 -
update
- 新值 - 返回:
- 如果成功,則返回 true。
getAndIncrement
public final int getAndIncrement()
-
以原子方式將當前值加 1。
-
- 返回:
- 以前的值
getAndDecrement
public final int getAndDecrement()
-
以原子方式將當前值減 1。
-
- 返回:
- 以前的值
getAndAdd
public final int getAndAdd(int delta)
-
以原子方式將給定值與當前值相加。
-
- 引數:
-
delta
- 要加上的值 - 返回:
- 以前的值
incrementAndGet
public final int incrementAndGet()
-
以原子方式將當前值加 1。
-
- 返回:
- 更新的值
decrementAndGet
public final int decrementAndGet()
-
以原子方式將當前值減 1。
-
- 返回:
- 更新的值
addAndGet
public final int addAndGet(int delta)
-
以原子方式將給定值與當前值相加。
-
- 引數:
-
delta
- 要加上的值 - 返回:
- 更新的值
toString
public String toString()
intValue
public int intValue()
longValue
public long longValue()
floatValue
public float floatValue()
-
從類
Number
複製的描述 -
以
float
形式返回指定的數值。這可能會涉及到舍入。 -
- 指定者:
-
類
Number
中的floatValue
-
- 返回:
-
轉換為
float
型別後該物件表示的數值。
doubleValue
public double doubleValue()
-
從類
Number
複製的描述 -
以
double
形式返回指定的數值。這可能會涉及到舍入。 -
- 指定者:
-
類
Number
中的doubleValue
-
- 返回:
-
轉換為
double
型別後該物件表示的數值。
轉自:http://www.cnblogs.com/tonylovett/p/5254509.html
在java併發程式設計中,會出現++,--等操作,但是這些不是原子性操作,這線上程安全上面就會出現相應的問題。因此java提供了相應類的原子性操作類。
1.AtomicInteger
可以用原子方式更新的 int
值。有關原子變數屬性的描述,請參閱 java.util.concurrent.atomic
包規範。AtomicInteger
可用在應用程式中(如以原子方式增加的計數器),並且不能用於替換 Integer
。但是,此類確實擴充套件了 Number
,允許那些處理基於數字類的工具和實用工具進行統一訪問。
構造方法:
1.AtomicInteger
public AtomicInteger(int initialValue)
-
建立具有給定初始值的新 AtomicInteger
- 引數:
-
initialValue
- 初始值
2.AtomicInteger
public AtomicInteger()
建立具有初始值 0
的新 AtomicInteger。
方法詳解:
get
public final int get()
-
獲取當前值。
-
- 返回:
- 當前值
set
public final void set(int newValue)
-
設定為給定值。
-
- 引數:
-
newValue
- 新值
lazySet
public final void lazySet(int newValue)
-
最後設定為給定值。
-
- 引數:
-
newValue
- 新值 - 從以下版本開始:
- 1.6
getAndSet
public final int getAndSet(int newValue)
-
以原子方式設定為給定值,並返回舊值。
-
- 引數:
-
newValue
- 新值 - 返回:
- 以前的值
compareAndSet
public final boolean compareAndSet(int expect, int update)
-
如果當前值
==
預期值,則以原子方式將該值設定為給定的更新值。 -
- 引數:
-
expect
- 預期值 -
update
- 新值 - 返回:
- 如果成功,則返回 true。返回 False 指示實際值與預期值不相等。
weakCompareAndSet
public final boolean weakCompareAndSet(int expect, int update)
-
如果當前值
==
預期值,則以原子方式將該設定為給定的更新值。可能意外失敗並且不提供排序保證,所以只有在很少的情況下才對
compareAndSet
進行適當地選擇。 -
- 引數:
-
expect
- 預期值 -
update
- 新值 - 返回:
- 如果成功,則返回 true。
getAndIncrement
public final int getAndIncrement()
-
以原子方式將當前值加 1。
-
- 返回:
- 以前的值
getAndDecrement
public final int getAndDecrement()
-
以原子方式將當前值減 1。
-
- 返回:
- 以前的值
getAndAdd
public final int getAndAdd(int delta)
-
以原子方式將給定值與當前值相加。
-
- 引數:
-
delta
- 要加上的值 - 返回:
- 以前的值
incrementAndGet
public final int incrementAndGet()
-
以原子方式將當前值加 1。
-
- 返回:
- 更新的值
decrementAndGet
public final int decrementAndGet()
-
以原子方式將當前值減 1。
-
- 返回:
- 更新的值
addAndGet
public final int addAndGet(int delta)
-
以原子方式將給定值與當前值相加。
-
- 引數:
-
delta
- 要加上的值 - 返回:
- 更新的值
toString
public String toString()
intValue
public int intValue()
longValue
public long longValue()
floatValue
public float floatValue()
-
從類
Number
複製的描述 -
以
float
形式返回指定的數值。這可能會涉及到舍入。 -
- 指定者:
-
類
Number
中的floatValue
-
- 返回:
-
轉換為
float
型別後該物件表示的數值。
doubleValue
public double doubleValue()
-
從類
Number
複製的描述 -
以
double
形式返回指定的數值。這可能會涉及到舍入。 -
- 指定者:
-
類
Number
中的doubleValue
-
- 返回:
-
轉換為
double
型別後該物件表示的數值。