java.util.concurrent.atomic
类 AtomicInteger
java.lang.Objectjava.lang.Number
java.util.concurrent.atomic.AtomicInteger
public class AtomicInteger extends Number implements Serializable
可以用原子方式更新的 int
值。有关原子变量属性的描述,请参阅 java.util.concurrent.atomic
包规范。AtomicInteger
可用在应用程序中(如以原子方式增加的计数器),并且不能用于替换 Integer
。但是,此类确实扩展了 Number
,允许那些处理基于数字类的工具和实用工具进行统一访问。
public AtomicInteger(int initialValue)
-
创建具有给定初始值的新 AtomicInteger。
-
参数:
-
initialValue
- 初始值
-
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
类型后该对象表示的数值。
-
转换为
import java.util.concurrent.atomic.AtomicInteger;
public class MyAtomicInteger {
private int value;
public MyAtomicInteger(int value) {
this.value = value;
}
public synchronized int increase() {
return value++;
}
public static void main(String args[]) {
long start = System.currentTimeMillis();
MyAtomicInteger test = new MyAtomicInteger(0);
for (int i = 0; i < 1000000; i++) {
test.increase();
}
long end = System.currentTimeMillis();
System.out.println("time elapse:" + (end - start));
long start1 = System.currentTimeMillis();
AtomicInteger atomic = new AtomicInteger(0);
for (int i = 0; i < 1000000; i++) {
atomic.incrementAndGet();
}
long end1 = System.currentTimeMillis();
System.out.println("time elapse:" + (end1 - start1));
}
}
输出结果
time elapse:33
time elapse:15
time elapse:15