AtomicLongFieldUpdater
AtomicLongFieldUpdater是正常Atomic类的内存优化版本,它牺牲了API的简洁性来换取内存占用的优化。通过该组件的单个实例就能支持某个类的多个实例,在我们的Record场景中,可以用它来更新volatile域
public class Record {
private static final AtomicLongFieldUpdater<Record> VERSION =
AtomicLongFieldUpdater.newUpdater(Record.class, "version");
private volatile long version = 0;
public long update() {
return VERSION.incrementAndGet(this);
}
public long get(){
return VERSION.get(this);
}
}