目录 一、说明二、代码示例 一、说明 1.利用字段更新器可以针对对象的域(Field) 2.只能配合volatile修饰的字段使用,否则会出异常Exception in thread “main” java.lang.IllegalArgumentException: Must be volatile type 3.常见有AtomicReferenceFieldUpdate、AtomicIntegerFieldUpdate、AtomicLongFieldUpdate 二、代码示例 package com.learning.atomic; import lombok.Data; import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; /** * @Author wangyouhui * @Description 原子字段更新 **/ public class AtomicFieldUpdate { public static void main(String[] args) { Student student = new Student(); AtomicReferenceFieldUpdater<Student, String> updater = AtomicReferenceFieldUpdater.newUpdater(Student.class, String.class, "name"); updater.compareAndSet(student, null, "张三"); System.out.println(student.getName()); } } @Data class Student{ volatile String name; }