您的位置:首页 > 编程语言 > Java开发

java.util.concurrent源码学习二

2016-07-14 15:31 375 查看
public class AtomicInteger extends Number implements java.io.Serializable
一:作用

An int value that may be updated atomically

二:成员变量

 与上一篇文章基本相同

三:主要成员方法,这里只展示AtomicInteger特有的方法

 将原来的值+1,并且返回修改前的值

public final int getAndIncrement() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
return current;
}
}


将原来的值-1,并且返回修改前的值

public final int getAndDecrement() {
for (;;) {
int current = get();
int next = current - 1;
if (compareAndSet(current, next))
return current;
}
}


将原来的值增加指定值,并返回修改后的值
/**
* Atomically adds the given value to the current value.
*
* @param delta the value to add
* @return the updated value
*/
public final int addAndGet(int delta) {
for (;;) {
int current = get();
int next = current + delta;
if (compareAndSet(current, next))
return next;
}
}


将原来的值-1,并返回修改后的值

/**
* Atomically decrements by one the current value.
*
* @return the updated value
*/
public final int decrementAndGet() {
for (;;) {
int current = get();
int next = current - 1;
if (compareAndSet(current, next))
return next;
}
}


将原来的值+1,并返回修改后的值

/**
* Atomically increments by one the current value.
*
* @return the updated value
*/
public final int incrementAndGet() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
return next;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: