原子类的了解与使用

原子类

一,什么是原子类

一度认为原子是不可再分割的最小单位,故原子类可以认为其操作都是不可分割。

二,为什么要有原子类

对多线程访问同一个变量,我们需要加锁,而锁是比较消耗性能的,JDK1.5之后,新增的原子操作提供一种用法简单,性能高效,线程安全地更新一个变量的方式。这些类位于atomic包下,发展到JDK1.8,该包下共有17个类,囊括原子更新基本类型,原子更新属性,原子更新引用。
在JDK1.8中新增的原子类为:DoubleAccumulator,DoubleAdder,LongAccumlator,LongAdder,striped64。

三,原子更新基本类型

大致可以分为三类:

1,AtomicBoolean,AtomicInteger,AtomicLong:属于元老级的原子更新,方法几乎一样。
2,DoubleAdder,LongAdder:对Double和long的原子更新性能进行优化提升。
3,DoubleAccumulator,LongAccumulator:支持自定义运算

AtomicInteger列子:

import java.util.concurrent.atomic.AtomicInteger;

/**
 * AtomicInteger
 * 对sum进行自增操作
 */
public class AtomicDemo {

    //相比较于 int sum = 0;
    private static AtomicInteger sum = new AtomicInteger(0);

    public static void increate(){
        sum.incrementAndGet();//相比较于sum++;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                for (int j = 0; j < 10; j++) {
                    increate();
                    System.out.println(sum);
                }
            }).start();
        }
    }
}

longAccumulator类型自定义运算列子:

import java.util.concurrent.atomic.LongAccumulator;

public class AtomicDemo2 {
    public static void main(String[] args) {
        LongAccumulator longAccumulator = new LongAccumulator((left,right)->
                left*right,1
                );

        longAccumulator.accumulate(3);
        System.out.println(longAccumulator.get());
    }
}

四, 原子更新数组类型

分别有AtomicintegerArray, AtomicLongArray,AtomicReferenceArray
AtomicintegerArray列子:

/**
 * 原子AtomicIntegerArray数组更新
 */

import java.util.concurrent.atomic.AtomicIntegerArray;

public class AtomicDemo3 {
    public static void main(String[] args) {
        int[] array = {2,3};
        AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(array);
        System.out.println("atomicIntegerArray = " + atomicIntegerArray.addAndGet(1,2));

        //3+2+5 = 10
        int i = atomicIntegerArray.accumulateAndGet(1,5,(left, right) ->
                left+right
                );
        System.out.println("i = " + i);
    }
}

五,原子更新属性

原子的更新某个类里的某个字段时,就需要使用更新字段类。atomic包下提供了以下四个类进行原子字段更新:
AtomicIntegerFiledUpdater,AtomicLongFieldUpdater,AtomicStampedReference,AtomicReferenceFieldUpdater
使用上述类的时候,必须遵循以下原则:
1,属性字段必须是volatile类型,在线程之间共享变量时立即可见。
2,字段的描述类型是与调用者与操作者对象字段的关系一致,也就是说调用者能够直接操作对象的字段,那么就可以进行原子类操作。
3,对于父类的字段,子类是不能直接操作的,尽管子类可以访问父类的字段。
4,只能是实例变量,不能是类变量,也就是说不能加static关键字修饰的,也不能是final修饰的变量。
5,对于AtomicIntegerFiledUpdater,AtomicLongFieldUpdater只能修改int/long类型字段,不能是其包装类,如果要修改其包装类需要用另外的类。
列子:
student实体类

public class Student {
    volatile long  id;
    volatile String name;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Student(long id, String name) {
        this.id = id;
        this.name = name;
    }

    public Student() {
    }
}
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

public class AtomicDemo4 {
    public static void main(String[] args) {
        //泛型填对应的实体类,参数为实体类的对象.class,与要修改的属性,且为字符串
        AtomicLongFieldUpdater<Student> atomicLongFieldUpdater =
                AtomicLongFieldUpdater.newUpdater(Student.class,"id");

        Student student = new Student(1L,"Thomas");
        //比较然后替换,修改的属性为long类型id
        atomicLongFieldUpdater.compareAndSet(student,1L,100L);
        System.out.println("student = " + student.getId());
        
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater =
                AtomicReferenceFieldUpdater.newUpdater(Student.class,String.class,"name");
        //修改的属性为String类型name
        atomicReferenceFieldUpdater.compareAndSet(student,"Thomas","Jack");
        System.out.println("student = " + student.getName());
    }
}

运行输出:
实例化时的值为id:1 name:Thomas
在这里插入图片描述

六,原子更新引用

AtomicReference:用于对引用的原子更新。
AtomicMarkableReference:带版本戳的原子引用类型,版本戳为Boolean类型。
AtomicStampedreference:带版本戳的原子引用类型,版本戳为int类型。

AtomicReference更新引用类型列子:
student实体类

public class Student {

    private long id;

    private String name;

    public Student(long id, String name) {
        this.id = id;
        this.name = name;
    }

    public Student() {
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

import java.util.concurrent.atomic.AtomicReference;

public class AtomicDemo5 {

    public static void main(String[] args) {
        AtomicReference<Student> atomicReference = new AtomicReference();
        Student student1 = new Student(1L,"Thomas");
        Student student2 = new Student(2L,"Jack");
		//不能直接将Thomas替换成Jack,需要经过以下步骤进行并获取
        atomicReference.set(student1);
        //把student1替换成student2
        atomicReference.compareAndSet(student1,student2);
        Student student = atomicReference.get();
        System.out.println(student.getName());
    }
}

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值