无锁之Atomic大杂烩

时间:2021-09-12 12:09:37

Atomic包共有12个类,可以分为四大类,基本类型的原子更新,数组的原子更新,引用的原子更新,volatile字段的原子更新。Atomic包里的类基本都是使用Unsafe实现的包装类。

基本类型的原子更新

AtomicBoolean
AtomicInteger
AtomicLong

数组的原子更新

AtomicIntegerArray
AtomicLongArray
AtomicReferenceArray

对象的原子更新

AtomicReference
AtomicMarkableReference
AtomicStampedReference // 原子更新带有版本号的引用类型,用于解决CAS时,可能出现的ABA问题

volatile字段的原子更新

AtomicIntegerFieldUpdater
AtomicLongFieldUpdater
AtomicReferenceFieldUpdater

AtomicInteger的主要方法

  • compareAndSet(int expect, int update) // 如果当前值 ==预期值,则以原子方式将该值设置为给定的更新值
  • addAndGet(int delta) // 以原子方式将给定值与当前值相加,返回新值
  • decrementAndGet() // 以原子方式将当前值减 1,返回新值
  • incrementAndGet() //以原子方式将当前值减 1,返回新值
  • set(int newValue) // 设置为给定值 get() //获取当前值

  • getAndAdd(int delta) // 以原子方式将给定值与当前值相加,返回旧值

  • getAndDecrement() //以原子方式将当前值减 1,返回旧值
  • getAndIncrement() // 以原子方式将当前值减 1,返回旧值
  • getAndSet(int newValue) // 以原子方式设置为给定值,并返回旧值

栗子

基本类型的原子更新

    static AtomicBoolean atomicBoolean=new AtomicBoolean(false);
static AtomicInteger atomicInteger=new AtomicInteger(1);
static AtomicLong atomicLong=new AtomicLong(1L);

public static void main(String[] args) {
atomicBoolean.set(true);
atomicInteger.incrementAndGet();
atomicLong.decrementAndGet();
}

数组的原子更新

    static int[] intArray=new int[]{1,2};
static long[] longArray=new long[]{1L,2L};
static String[] stringArray=new String[]{"1","2"};

static AtomicIntegerArray atomicIntegerArray=new AtomicIntegerArray(intArray);
static AtomicLongArray atomicLongArray=new AtomicLongArray(longArray);
static AtomicReferenceArray atomicReferenceArray=new AtomicReferenceArray(stringArray);

public static void main(String[] args) {
atomicIntegerArray.set(0, 2);
atomicLongArray.set(0, 2L);
atomicReferenceArray.set(0, "2");
}

对象的原子更新

    static AtomicReference atomicReference=new AtomicReference();
static String ref1="aaa";
static AtomicMarkableReference atomicMarkableReference=new AtomicMarkableReference<String>(ref1,false);

static Integer ref2=111;
static AtomicStampedReference atomicStampedReference=new AtomicStampedReference<Integer>(ref2, 0);

public static void main(String[] args) {
Demo demo1=new Demo("aaa","111");
Demo demo2=new Demo("bbb","222");
atomicReference.set(demo1);
atomicReference.compareAndSet(demo1,demo2);

if(atomicMarkableReference.isMarked()!=true){
atomicMarkableReference.set("bbb", true);
};

if(atomicStampedReference.getStamp()==0){
atomicStampedReference.set(new Integer("222"), 1);
}
}

static class Demo{
public Demo(String name,String address){
this.name=name;
this.address=address;
}
private String name;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}

volatile字段的原子更新

    static AtomicIntegerFieldUpdater atomicIntegerFieldUpdater=AtomicIntegerFieldUpdater.newUpdater(Demo.class, "v1");
static AtomicLongFieldUpdater atomicLongFieldUpdater=AtomicLongFieldUpdater.newUpdater(Demo.class, "v2");
static AtomicReferenceFieldUpdater atomicReferenceFieldUpdater=AtomicReferenceFieldUpdater.newUpdater(Demo.class, String.class, "v3");

public static void main(String[] args) {
Demo demo1=new Demo();
atomicIntegerFieldUpdater.incrementAndGet(demo1);
atomicLongFieldUpdater.incrementAndGet(demo1);
atomicReferenceFieldUpdater.set(demo1, "aaa");
System.out.println(demo1.v1);
System.out.println(demo1.v2);
System.out.println(demo1.v3);
}

static class Demo{
public volatile int v1; //不能用Integer
public volatile long v2; //不能用Long
public volatile String v3;
}