AtomicIntegerArray源码详解
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了AtomicIntegerArray源码详解,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4485字,纯文字阅读大概需要7分钟。
内容图文
AtomicIntegerArray源码详解
AtomicIntegerArray主要提供原子性操作int数据类型数组元素的操作。
类定义
public class AtomicIntegerArray implements java.io.Serializable{}
属性定义
private static final long serialVersionUID = 2862133569453604235L;
private static final Unsafe unsafe = Unsafe.getUnsafe();
private static final int base = unsafe.arrayBaseOffset(int[].class);
private static final int shift;
private static int[] array;
base属性的含义是,数组中第一个元组在数组类中的偏移量。
shift属性的含义是,整型元素中,第一个为1的二进制位距离最高位的偏移量。
array属性就是底层,封装的真正的数组。
静态属性初始化
static {
int scale = unsafe.arrayIndexScale(int[].class);
if((scale & (scale - 1)) != 0){
throw new Error("data type scale not a power of two");
}
shift = 31 - Integer.numberOfLeadingZeros(scale);
}
构造函数
public AtomicIntegerArray(int length){
array = new int[length];
}
public AtomicIntegerArray(int[] array){
this.array = array.clone();
}
第一个构造函数,根据给定的长度创建一个AtomicIntegerArray实例,而且数组的元素都为零。
第二个构造函数,根据一个给定的数组对象创建AtomicIntegerArray对象,而且数据元素是给定数组的元素的拷贝。
工具方法
private long checkedByteOffset(int i){
if(i < 0 || i >= array.length){
throw new IndexOutOfBoundException("index " + i);
}
return byteOffset(i);
}
private static long byteOffset(int i){
return ((long)i << shift) + base;
}
checkedByteOffset方法用于对数组元素的下标范围进行检查,如果通过检查,那么返回该下标所对应的元素,在数组类中的偏移。
获取数组长度的方法
public final int length(){
return array.length;
}
toString方法
public String toString(){
int iMax = array.length - 1;
if(iMax == -1){
return "[]";
}
StringBuilder b = new StringBuilder();
b.append('[');
for(int i = 0; ; i++){
b.append(getRaw(byteOffset(i)));
if(i == iMax){
return b.append(']').toString();
}
b.append(',').append(' ');
}
}
get和set方法
public final int get(int i){
return getRaw(checkedByteOffset(i));
}
private int getRaw(long offset){
return unsafe.getIntVolatile(array, offset);
}
public final void set(int i, int newValue){
unsafe.putIntVolatile(array, checkedByteOffset(i), newValue);
}
get方法,获取指定下标处的元素的值。
getRaw方法,获取AtomicIntegerArray类中,指定偏移处的元素值。
set方法,设置AtomicIntegerArray中的数组中下标为i处,元素的值。
lazySet方法
public final void lazySet(int i, int newValue){
unsafe.putOrderInt(array, checkedByteOffset(i), newValue);
}
获取设置接口
public final int getAndSet(int i, int newValue){
return unsafe.getAndSetInt(array, checkedByteOffset(i), newValue);
}
把数组中下标为i的元素的值设置为newValue,然后返回原来的值。
比较设置方法
public final boolean compareAndSet(int i, int expect, int update){
return compareAndSetRaw(checkedByteOffset(i), expect, update);
}
public boolean compareAndSetRaw(long offset, int expect, int update){
return unsafe.compareAndSwapInt(array, offset, expect, update);
}
public final boolean weakCompareAndSet(int i, int expect, int update){
return compareAndSet(i, expect, update);
}
获取自增方法
public final int getAndIncreament(int i){
return getAndAdd(i, 1);
}
public final int increamentAndGet(int i){
return getAndAdd(i, 1) + 1;
}
获取自减方法
public final int getAndDecreament(int i){
return getAndAdd(i, -1);
}
public final int decrementAndGet(int i){
return getAndAdd(i, -1) - 1;
}
获取增加方法
public final int getAndAdd(int i, int delta){
return unsafe.getAndAddInt(array, checkedByteOffset(i), delta);
}
public final int addAndGet(int i, int delta){
return getAndAdd(i, delta) + delta;
}
获取更新方法
public final int getAndUpdate(int i, IntUnaryOperator updateFunction){
long offset = checkedByteOffset(i);
int prev, next;
do{
prev = getRaw(offset);
next = updateFunction.applyAsInt(prev);
}while(!compareAndSetRaw(offset, prev, next));
return prev;
}
public final int updateAndGet(int i, IntUnaryOperator updateFunction){
long offset = checkedByteOffset(i);
int prev, next;
do{
prev = getRaw(offset);
next = updateFunction.applyAsInt(prev);
}while(!compareAndSetRaw(offset, prev, next));
return next;
}
获取累加方法
public final int getAndAccumulate(int i, int x, IntBinaryOperator accumulatorFunction){
long offset = checkedByteOffset(i);
int prev, next;
do{
prev = getRaw(offset);
next = accumulatorFunction.applyAsInt(prev, x);
}while(!compareAndSetRaw(offset, prev, next));
return prev;
}
public final int accumulateAndGet(int i, int x, IntBinaryOperator accumulatorFunction){
long offset = checkedByteOffset(i);
int prev, next;
do{
prev = getRaw(offset);
next = accumulatorFunction.applyAsInt(prev, x);
}while(!compareAndSetRaw(offset, prev, next));
return next;
}
内容总结
以上是互联网集市为您收集整理的AtomicIntegerArray源码详解全部内容,希望文章能够帮你解决AtomicIntegerArray源码详解所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。