原子类

原子类

对象的数据操作不可分割的。具有原子性,保证了并发下线程安全问题。存在于package java.util.concurrent.atomic 原子包下。

Number抽象类

提供了基本数字类型之间相互转换的方法。使用java自带的强制类型转换(long、int····)获得返回类型,涉及到截断和舍入。

public abstract class Number implements java.io.Serializable {
	public byte byteValue() {
        return (byte)intValue();
    }
    public short shortValue() {
        return (short)intValue();
    }
    public abstract int intValue();
    public abstract long longValue();
    public abstract float floatValue();
    public abstract double doubleValue();
}

基本类型

AtomicInteger

底层通过Unsafe类实现,objectFieldOffset方法获得value字段在AtomicInteger类中的位偏移,compareAndSwapInt方法CAS修改value值。

public class AtomicInteger extends Number implements java.io.Serializable {
	private volatile int value;//持有的实际值

	//构造方法,初始value值(未赋值为0)
	public AtomicInteger() {
    }
    public AtomicInteger(int initialValue) {
        value = initialValue;
    }

	//常见方法get、set
	//CAS方法
	public final void lazySet(int newValue) {
		//使volatile修饰的变量修改后对其他线程非立即可见
        unsafe.putOrderedInt(this, valueOffset, newValue);
    }
    //获得当前值并设置新值
    public final int getAndSet(int newValue) {
        return unsafe.getAndSetInt(this, valueOffset, newValue);
    }
    //返回当前值并将value自增自减
    public final int getAndIncrement() {
        return unsafe.getAndAddInt(this, valueOffset, 1);
    }
    public final int getAndDecrement() {
        return unsafe.getAndAddInt(this, valueOffset, -1);
    }
    //获得当前值并将value加上指定值
    public final int getAndAdd(int delta) {
        return unsafe.getAndAddInt(this, valueOffset, delta);
    }
    //获得自增后的值并自增value
    public final int incrementAndGet() {
        return unsafe.getAndAddInt(this, valueOffset, 1) + 1;
    }
}

数组类型

AtomicIntegerArray

整形数组原子类

public class AtomicIntegerArray implements java.io.Serializable {
	//类初始化时加载
	static {
		//获得整形数组中每个元素的位偏移量,此处scale为4
        int scale = unsafe.arrayIndexScale(int[].class);
        //scale不为2的幂则抛出异常
        if ((scale & (scale - 1)) != 0)
            throw new Error("data type scale not a power of two");
        //获得偏移,31-scale最高位前面有多少个0,此处shift为2,通过移位来获取数组中元素位置
        shift = 31 - Integer.numberOfLeadingZeros(scale);
    }


	private final int[] array;//实际存放

	//构造方法,传入数组则调用其clone方法
	public AtomicIntegerArray(int length) {
        array = new int[length];
    }
    public AtomicIntegerArray(int[] array) {
        // Visibility guaranteed by final field guarantees
        this.array = array.clone();
    }

	//get、set使用volatile语义
	public final void set(int i, int newValue) {
        unsafe.putIntVolatile(array, checkedByteOffset(i), newValue);
    }
    public final int get(int i) {
        return getRaw(checkedByteOffset(i));
    }
	//CAS操作,自旋直到修改成功
	public final int getAndSet(int i, int newValue) {
		//获得索引i处的值并修改
        return unsafe.getAndSetInt(array, checkedByteOffset(i), newValue);
    }
    public final int getAndAdd(int i, int delta) {
    	//获得索引i处的值并添加delta值
        return unsafe.getAndAddInt(array, checkedByteOffset(i), delta);
    }
    //CAS设置新值,单次
    public final boolean compareAndSet(int i, int expect, int update) {
        return compareAndSetRaw(checkedByteOffset(i), expect, update);
    }
}

引用类型

AtomicReference

引用类型原子类,类似简单类型原子类,只是CAS控制的参数不同,此类控制的是引用值。

AtomicStampedRerence

带邮戳的引用类型原子类,解决了CAS的ABA问题。

内部类Pair

AtomicStampedRerence内部定义了一个Pair类来存放引用值及其对应的邮戳

private static class Pair<T> {
    final T reference;
    final int stamp;
    private Pair(T reference, int stamp) {
        this.reference = reference;
        this.stamp = stamp;
    }
    static <T> Pair<T> of(T reference, int stamp) {
        return new Pair<T>(reference, stamp);
    }
}
基本结构
private volatile Pair<V> pair;

//构造方法
public AtomicStampedReference(V initialRef, int initialStamp) {
    pair = Pair.of(initialRef, initialStamp);
}

//cas操作
public boolean compareAndSet(V   expectedReference,
                                 V   newReference,
                                 int expectedStamp,
                                 int newStamp) {
    Pair<V> current = pair;
    return
    	//原引用未变
        expectedReference == current.reference &&
        //原邮戳未变
        expectedStamp == current.stamp &&
        //新引用和邮戳与原引用和邮戳相同 或 CAS修改Pair成功
        ((newReference == current.reference &&
          newStamp == current.stamp) ||
         casPair(current, Pair.of(newReference, newStamp)));
}
//CAS修改邮戳
public boolean attemptStamp(V expectedReference, int newStamp) {
    Pair<V> current = pair;
    return
        expectedReference == current.reference &&
        (newStamp == current.stamp ||
         casPair(current, Pair.of(expectedReference, newStamp)));
}
AtomicMarkableReference

与AtomicStampedReference不同,改类内部类维持了一个boolean变量来判断是否修改过引用值,只是降低了ABA问题发生的概率。

内部类Pair
private static class Pair<T> {
    final T reference;
    final boolean mark;
    private Pair(T reference, boolean mark) {
        this.reference = reference;
        this.mark = mark;
    }
    static <T> Pair<T> of(T reference, boolean mark) {
        return new Pair<T>(reference, mark);
    }
}

属性更新器类型

AtomicReferenceFieldUpdater

原子更新volatile修饰的引用类型里的字段的更新器,不能作用于static或final修饰的变量。实现非侵入式修改类中字段。

//持有该字段的对象的类+该字段的类+要更新的字段的名称 
public static <U,W> AtomicReferenceFieldUpdater<U,W> newUpdater(Class<U> tclass,
                                                                    Class<W> vclass,
                                                                    String fieldName) {
    return new AtomicReferenceFieldUpdaterImpl<U,W>
        (tclass, vclass, fieldName, Reflection.getCallerClass());
}

//实际调用的方法
AtomicReferenceFieldUpdaterImpl(final Class<T> tclass,
                                        final Class<V> vclass,
                                        final String fieldName,
                                        final Class<?> caller) {
    final Field field;
    final Class<?> fieldClass;
    final int modifiers;
    try {
    	//通过反射获得要更新的Field
        field = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Field>() {
                public Field run() throws NoSuchFieldException {
                    return tclass.getDeclaredField(fieldName);
                }
            });
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(
            caller, tclass, null, modifiers);
        ClassLoader cl = tclass.getClassLoader();
        ClassLoader ccl = caller.getClassLoader();
        if ((ccl != null) && (ccl != cl) &&
            ((cl == null) || !isAncestor(cl, ccl))) {
            sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        }
        fieldClass = field.getType();
    } catch (PrivilegedActionException pae) {
        throw new RuntimeException(pae.getException());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
	//如果传入字段类型与反射获得的类型不匹配
    if (vclass != fieldClass)
        throw new ClassCastException();
    //如果字段为原始类型
    if (vclass.isPrimitive())
        throw new IllegalArgumentException("Must be reference type");

    //如果引用不被volatile修饰
    if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");
    this.cclass = (Modifier.isProtected(modifiers) &&
                   tclass.isAssignableFrom(caller) &&
                   !isSamePackage(tclass, caller))
                  ? caller : tclass;
    this.tclass = tclass;
    this.vclass = vclass;
    //获得修改字段的位偏移
    this.offset = U.objectFieldOffset(field);
}


//CAS修改字段
public final boolean compareAndSet(T obj, V expect, V update) {
    //obj和传入值类型检查
    accessCheck(obj);
    valueCheck(update);
    return U.compareAndSwapObject(obj, offset, expect, update);
}

Adder累加器

一个初始值为0的累加器,在高并发下比AtomicLong更加高效

  1. 如果cell为null直接CAS修改base值
  2. 如果getProbe() & m(根据线程threadLocalRandomProbe计算出的位置)不为null则cas修改cell对应位置的值
  3. 初始化cell大小为2并cas设置值,如果已初始化对应位置为null则设置值
  4. 如果是多个线程对同一位置的cell进行操作则对数组扩容一倍
public class LongAdder extends Striped64 implements Serializable {
//累加器基础值
transient volatile long base;
transient volatile Cell[] cells;

public void add(long x) {
    Cell[] as; long b, v; int m; Cell a;
    //如果cell为null直接CAS修改base值
    if ((as = cells) != null || !casBase(b = base, b + x)) {
        boolean uncontended = true;
        if (as == null || (m = as.length - 1) < 0 ||
            (a = as[getProbe() & m]) == null ||
            !(uncontended = a.cas(v = a.value, v + x)))
            longAccumulate(x, null, uncontended);
    }
}
}

Accumulator累加器

LongAccumulator 是 LongAdder 的一个更通用的版本,围绕LongBinaryOperator类型的lambda表达式构建

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值