-
AtomicReferenceFieldUpdater
-
AtomicIntegerFieldUpdater
-
AtomicLongFieldUpdater
利用字段更新器,可以针对对象的某个域(Field)进行原子操作,只能配合volatile使用,否则会出现异常
package com.sharing_model.no_lock;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
/**
* 字段更新器:
* AtomicReferenceFieldUpdater
* AtomicIntegerFieldUpdater
* AtomicLongFieldUpdater
*
* 利用字段更新器,可以针对对象的某个域(Field)进行原子操作,只能配合volatile使用,否则会出现异常
*/
public class AtomicReferenceUpdaterTest {
public static void main(String[] args) {
Student student = new Student();
AtomicReferenceFieldUpdater<Student, String> updater =
AtomicReferenceFieldUpdater.newUpdater(Student.class, String.class, "name");
System.out.println(updater.compareAndSet(student, null, "张三"));
}
}
class Student {
volatile String name;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
'}';
}
}