为此,Java并发包(JUC)提供了AtomicLongFieldUpdater<T>——AtomicIntegerFieldUpdater的64位兄弟。 文章被收录于专栏:云时代Java开发:原理、实战与优化第一章:设计哲学——为何需要AtomicLongFieldUpdater? 1.3AtomicLongFieldUpdater的核心价值AtomicLongFieldUpdater提供了一个近乎完美的折中方案:展开代码语言:TXTAI代码解释publicclassSpan{// 重要提示:虽然AtomicLongFieldUpdater保证了单个字段的原子性,但它不保证对整个对象的操作是原子的。 解决方案:为Agent的关键指标字段使用AtomicLongFieldUpdater。
对象属性类型的原子类 对象属性类型的原子类包含:AtomicIntegerFieldUpdater、AtomicLongFieldUpdater和AtomicReferenceFieldUpdater newUpdater方法 public static AtomicIntegerFieldUpdater newUpdater(Class tclass, String fieldName) //AtomicLongFieldUpdater 的newUpdater方法 public static AtomicLongFieldUpdater newUpdater(Class tclass, String fieldName //AtomicIntegerFieldUpdater的compareAndSet()方法 compareAndSet(T obj, int expect, int update) //AtomicLongFieldUpdater 另外,需要注意的是:使用AtomicIntegerFieldUpdater、AtomicLongFieldUpdater和AtomicReferenceFieldUpdater更新对象的属性时,对象属性必须是
} } 4.原子地更新属性 原子地更新某个类里的某个字段时,就需要使用原子更新字段类,Atomic包提供了以下4个类进行原子字段更新 AtomicIntegerFieldUpdater、AtomicLongFieldUpdater this.name = name; } } public static void main(String[] args) { AtomicLongFieldUpdater <Student> longFieldUpdater = AtomicLongFieldUpdater.newUpdater(Student.class, "id"); Student
} } 2 锁的对象本身大小 减少空间占用: public final class ChannelOutboundBuffer { private static final AtomicLongFieldUpdater <ChannelOutboundBuffer> TOTAL_PENDING_SIZE_UPDATER = AtomicLongFieldUpdater.newUpdater(ChannelOutboundBuffer.class
private volatile long lock; public void acquireLock(); public void releaseeLock(); } 使用 AtomicLongFieldUpdater 目前Java提供了两种公共API,可以实现这种CAS操作,比如使用 java.util.concurrent.atomic.AtomicLongFieldUpdater,它是基于反射机制创建,我们需要保证类型和字段名称正确 private static final AtomicLongFieldupdater<AtomicBTreePartition> lockFieldUpdater = AtomicLongFieldupdater.newUpdater
AtomicLongFieldUpdater:原子更新某个类的volatile长整型字段。
原子更新字段类 AtomicIntegerFieldUpdater:原子更新整型的字段的更新器 AtomicLongFieldUpdater:原子更新长整型字段的更新器 AtomicStampedReference
如AtomicIntegerFieldUpdater类和AtomicLongFieldUpdater类,前者就是更新Integer类型的字段后者就是更新Long类型字段的。
AtomicLong AtomicReference 二类 AtomicMarkableReference AtomicStampedReference 三类 AtomicIntegerFieldUpdater AtomicLongFieldUpdater
其中的类可以分成4组 AtomicBoolean,AtomicInteger,AtomicLong,AtomicReference AtomicIntegerArray,AtomicLongArray AtomicLongFieldUpdater AtomicIntegerFieldUpdater<T>/AtomicLongFieldUpdater<T>/AtomicReferenceFieldUpdater<T,V>是基于反射的原子更新字段的值 (5)对于AtomicIntegerFieldUpdater和AtomicLongFieldUpdater只能修改int/long类型的字段,不能修改其包装类型(Integer/Long)。
Long> actual; final Worker worker; volatile long requested; static final AtomicLongFieldUpdater <IntervalRunnable> REQUESTED = AtomicLongFieldUpdater.newUpdater(IntervalRunnable.class
AtomicIntegerArray/AtomicLongArray/AtomicReferenceArray 提供对对应类型数组的原子更新 2.4 AtomicIntegerFieldUpdater/AtomicLongFieldUpdater private 对于父类的字段,子类是不能直接操作的,尽管子类可以访问到父类的字段 只要是实例变量,不能是类变量,也就是说不能加static关键字 对于AtomicIntegerFieldUpdater 和AtomicLongFieldUpdater
AtomicBoolean AtomicInteger AtomicIntegerArray AtomicIntegerFieldUpdater AtomicLong AtomicLongArray AtomicLongFieldUpdater 接着AtomicReferenceFieldUpdater, AtomicIntegerFieldUpdater和AtomicLongFieldUpdater 基于反射能力可以在指定的类里面指定的volatile
4、Atomic\FieldUpdater(原子更新属性) Atomic\FieldUpdater 原子更新属性,包括三种:AtomicIntegerFieldUpdater、AtomicLongFieldUpdater 但如果是一个已经有的类,在不能更改其源代码的情况下,要想实现对其成员变量的原子操作,就需要使用 AtomicIntegerFieldUpdater、AtomicLongFieldUpdater、AtomicReferenceFieldUpdater
目前Java提供了两种公共API,可以实现这种CAS操作,比如使用java.util.concurrent.atomic.AtomicLongFieldUpdater,它是基于反射机制创建,我们需要保证类型和字段名称正确 private static final AtomicLongFieldUpdater<AtomicBTreePartition> lockFieldUpdater = AtomicLongFieldUpdater.newUpdater
AtomicLong, AtomicReference 原子数组:AtomicIntegerArray, AtomicLongArray,AtomicReferenceArray 原子属性更新器: AtomicLongFieldUpdater
类中某一字段原子处理 AtomicIntegerFieldUpdater AtomicLongFieldUpdater AtomicReferenceFieldUpdater 要求CAS操作字段是非static
AtomicLongFieldUpdater:原子更新volatile修饰的长整型字段的更新器。 AtomicLongFieldUpdater:原子更新volatile修饰的长整型字段的更新器。
原子更新引用类型里的字段原子类 AtomicMarkableReference :原子更新带有标记位的引用类型 对象的属性修改类型 AtomicIntegerFieldUpdater:原子更新整形字段的更新器 AtomicLongFieldUpdater
AtomicIntegerArray、AtomicLongArray、AtomicReferenceArray 属性原子修改器(Updater): AtomicIntegerFieldUpdater、AtomicLongFieldUpdater